Description

The JavaScript String method localeCompare() compares a given string with another string, in sort order, and returns the below values.

  • Returns 1 if the given string comes after the reference string.
  • Returns -1 if the given string comes after the reference string.
  • Returns 0 if the given string comes is equal to the reference string.

Syntax

The method localeCompare() has the below syntax, where str is a string.

str.localeCompare(compareString, locales, options)

Parameters

The method localeCompare() accepts the below parameters.

compareString

  • It is the string to be compared with the string str.

locales (optional)

  • It is used to define a language formatting convention for the comparison.
  • It helps in comparing strings with different languages.

options (optional)

  • It is used to pass additional formatting conventions for the comparison.

Result

The method localeCompare() compares the strings in sort order and returns the below values.

  • Returns 1 if the given string comes after the reference string.
  • Returns -1 if the given string comes after the reference string.
  • Returns 0 if the given string comes is equal to the reference string.

Example 1: Using the Method

The below example shows the basic use of this method.

// Comparing strings
document.write("a".localeCompare("b"));    // Prints: -1
document.write("b".localeCompare("a"));    // Prints: 1
document.write("b".localeCompare("b"));    // Prints: 0

// Comparing strings
document.write("Hello".localeCompare("World"));    // Prints: -1
document.write("Hello".localeCompare("Arun"));     // Prints: 1
document.write("Hello".localeCompare("Hello"));    // Prints: 0

Output:

-1
1
0

-1
1
0

Example 2: Using the Method with Locales Parameter

The below example shows the comparisons using the locales parameter, which specifies the language.

// Comparing without locale parameter
document.write("ä".localeCompare("z"));          // Prints: -1

// Comparing with locale parameter that specifies the German formatting convention
document.write("ä".localeCompare("z", "de"));    // Prints: -1

// Comparing with locale parameter that specifies the Swedish formatting convention
document.write("ä".localeCompare("z", "sv"));    // Prints: 1

Output:

-1
-1
1

Example 3: Using the Method with Options Parameter

The below example shows the comparisons using the options parameter, which specifies numeric values comparison (additional comparison details).

// Comparing with options to specify a numeric comparison
document.write("10".localeCompare("20", undefined, { numeric: true }));    // Prints: -1
document.write("20".localeCompare("10", undefined, { numeric: true }));    // Prints: 1
document.write("20".localeCompare("20", undefined, { numeric: true }));    // Prints: 0

Output:

-1
1
0

Overall

The JavaScript method localeCompare() compares two strings and returns a numeric value that determines whether a string comes before, after, or equal to the other string, in sort order.

Related Links