Javascript String localeCompare()

The localeCompare() method checks if a given string comes before, after, or is equivalent as another string in sort order.

Example

// comparing 'c' with 'b' using localeCompare() let result1 = 'c'.localeCompare('b');
// returns positive value if 'b' comes before 'c' console.log(result1); // Output: // 1

localeCompare() Syntax

The syntax of the localeCompare() method is:

str.localeCompare(compareStr, locales, options)

Here, str is a string.


localeCompare() Parameters

The localeCompare() method takes in:

  • compareStr - The string against which str is compared.
  • locales and options (optional) - These arguments customize function by specifying what formatting conventions to use.

localeCompare() Return Value

The localeCompare() returns:

  • -1 : if the reference string is sorted before compareStr.
  • 0 : if two strings are equivalent.
  • 1 : if the reference string is sorted after compareStr.

Note: Since the returned negative and positive integers vary between browsers, do not rely on exact values -1 or 1.


Example 1: Using localeCompare() Method

// comparing 'c' with 'b' using localeCompare() let result1 = 'c'.localeCompare('b')
; console.log(result1);

Output

1

In the above example, we have passed 'c' as a reference string and 'b' as a compare string and have assigned the return value of localeCompare() to result1.

Since the alphabet 'c' comes after 'b', 'c'.localeCompare('b') returns a positive number i.e. 1.


Example 2: Using localeCompare()

// comparing 'c' with 'b' using localeCompare() let result2 = 'b'.localeCompare('c');
console.log(result2);

Output

-1

Here, we have passed 'b' as a reference string and 'c' as a compare string. Since the reference string comes before compareStr, 'b'.localeCompare('c') returns a negative value which is -1.


Example 3: localeCompare() With Equal Strings

// comparing 'JavaScript' and 'JavaScript' using localeCompare() let result1 = 'JavaScript'.localeCompare('JavaScript');
console.log(result1);
// comparing 'Python' and 'JavaScript' using localeCompare() let result2 = 'Python'.localeCompare('JavaScript');
console.log(result2);

Output

0
1

In the above example, we have compared two unequal strings 'Python' and 'JavaScript'. Since 'Python' comes after 'JavaScript', the method returns 1.

However, while comparing two equal strings 'JavaScript', the method returns 0.


Example 4: Using localeCompare() With locales and options

// passing locale that specifies the German formatting convention let value1 = "ä".localeCompare("z", "de");
console.log(value1); // a negative value: in German, ä sorts before z
// passing locale that specifies the Swedish formatting convention let value2 = "ä".localeCompare("z", "sv");
console.log(value2); // a positive value: in Swedish, ä sorts after z // default comparison between two numbers '5' and '40' console.log("5".localeCompare("40")); // 1
// the last parameters indicates 'options' let value3 = "5".localeCompare("40", undefined, { numeric: true });
console.log(value3); // a negative value

Output

-1
1
1
-1

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges