Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Definition and Syntax
2.1.
Syntax:
2.2.
Return Value
3.
Basic Example
3.1.
Javascript code
3.2.
Usage with Locales
3.3.
Javascript code
3.4.
Using Options
3.5.
Javascript code
3.6.
Here, accents are considered, so the comparison results in 1.Case Sensitivity
3.7.
Javascript code
4.
Frequently Asked Questions
4.1.
Q: Can I use localeCompare() for case-insensitive comparison?
4.2.
Q: What if I pass an invalid locale?
4.3.
Q: Does localeCompare() modify the original string?
5.
Conclusion
Last Updated: Mar 27, 2024

JavaScript String localeCompare() Method

Author Sinki Kumari
0 upvote

Introduction

In JavaScript, strings are an integral part of everyday coding. There are several methods available to manipulate and compare strings, and one such valuable method is the localeCompare(). 

JavaScript String localeCompare() Method

This article introduces the localeCompare() method, explains its syntax and parameters, and provides real-world examples to demonstrate its functionality.

Definition and Syntax

The localeCompare() method in Javascript compares two strings in the current locale. It's used to determine the sort order of these strings.

Syntax:

string1.localeCompare(string2[, locales[, options]]);

string1: The string you are calling the method on.

string2: The string against which you want to compare.

locales (optional): A string or an array of strings representing the locale.

options (optional): An object with additional properties to define the comparison.

Return Value

The method returns an integer:

  • -1: If string1 comes before string2
     
  • 0: If the strings are equal
     
  • 1: If string1 comes after string2
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Basic Example

 

  • Javascript code

Javascript code

var string1 = "apple";
var string2 = "banana";
console.log(string1.localeCompare(string2)); 

Output:

 -1
Output

Usage with Locales

The localeCompare() method can consider local language rules in comparison.

Example:

  • Javascript code

Javascript code

var string1 = "ä";

var string2 = "z";

console.log(string1.localeCompare(string2, 'de')); //

 Output:

 -1
output

In German, "ä" comes before "z", so the output is -1.

Using Options

You can use an options object to specify the type of comparison.

Example:

  • Javascript code

Javascript code

var string1 = "résùmé";

var string2 = "resume";

var options = { sensitivity: 'accent' };

console.log(string1.localeCompare(string2, undefined, options)); 

Output:

 1

Here, accents are considered, so the comparison results in 1.
Case Sensitivity

  • Javascript code

Javascript code

var string1 = "apple";

var string2 = "Apple";

console.log(string1.localeCompare(string2)); // 

Output: 

1
output

By default, the comparison is case-sensitive.

Frequently Asked Questions

Q: Can I use localeCompare() for case-insensitive comparison?

Yes, you can set { sensitivity: 'base' } in the options for case-insensitive comparison.

Q: What if I pass an invalid locale?

If an invalid locale is passed, the method uses the runtime’s default locale.

Q: Does localeCompare() modify the original string?

No, localeCompare() does not modify the original strings. It only compares them.

Conclusion

The JavaScript localeCompare() method is a powerful tool for string comparison, allowing for localized and customizable comparisons. Its ability to compare strings according to language rules and specific options makes it versatile and essential for developers working on applications that need to support different languages and cultural rules. Understanding how to use localeCompare() effectively can enhance both the functionality and user experience of your applications. Whether you are building a sorting algorithm or just need to compare two strings, localeCompare() is a reliable, robust method to consider in your developer toolkit.

You may refer to our Guided Path on Code Ninjas Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning!

Previous article
Const in JavaScript
Next article
JavaScript Array forEach() Method
Live masterclass