JavaScript Tutorials

JavaScript String localeCompare()
Syntax & Examples

String.localeCompare() method

The localeCompare() method of the String class in JavaScript returns a number indicating whether the reference string compareString comes before, after, or is equivalent to the given string in sort order.


Syntax of String.localeCompare()

There are 3 variations for the syntax of String.localeCompare() method. They are:

1.
localeCompare(compareString)

Parameters

ParameterOptional/RequiredDescription
compareStringrequiredThe string against which the reference string is compared.

This method returns a number indicating whether the reference string compareString comes before, after, or is equivalent to the given string in sort order.

Returns value of type Number.

2.
localeCompare(compareString, locales)

Parameters

ParameterOptional/RequiredDescription
compareStringrequiredThe string against which the reference string is compared.
localesoptionalA string with a BCP 47 language tag, or an array of such strings, to specify the locale to use for sorting.

This method returns a number indicating whether the reference string compareString comes before, after, or is equivalent to the given string in sort order, using locale-specific sort order for the given locales.

Returns value of type Number.

3.
localeCompare(compareString, locales, options)

Parameters

ParameterOptional/RequiredDescription
compareStringrequiredThe string against which the reference string is compared.
localesoptionalA string with a BCP 47 language tag, or an array of such strings, to specify the locale to use for sorting.
optionsoptionalAn object with configuration properties to control the comparison.

This method returns a number indicating whether the reference string compareString comes before, after, or is equivalent to the given string in sort order, using locale-specific sort order for the given locales and options.

Returns value of type Number.



✐ Examples

1 Using localeCompare() method for basic comparison

In JavaScript, we can use the localeCompare() method to compare two strings and determine their sort order.

For example,

  1. We define two string variables, str1 with the value 'apple' and str2 with the value 'banana'.
  2. We use the localeCompare() method to compare str1 and str2.
  3. The result is stored in the variable comparison.
  4. We log comparison to the console using the console.log() method.

JavaScript Program

const str1 = 'apple';
const str2 = 'banana';
const comparison = str1.localeCompare(str2);
console.log(comparison);

Output

-1

2 Using localeCompare() method with locales

In JavaScript, we can use the localeCompare() method to compare two strings using locale-specific sort order.

For example,

  1. We define two string variables, str1 with the value 'äpfel' and str2 with the value 'banana'.
  2. We use the localeCompare() method with the arguments str2 and 'de' to compare str1 and str2 using German locale.
  3. The result is stored in the variable comparison.
  4. We log comparison to the console using the console.log() method.

JavaScript Program

const str1 = 'äpfel';
const str2 = 'banana';
const comparison = str1.localeCompare(str2, 'de');
console.log(comparison);

Output

1

3 Using localeCompare() method with locales and options

In JavaScript, we can use the localeCompare() method to compare two strings using locale-specific sort order and comparison options.

For example,

  1. We define two string variables, str1 with the value 'apple' and str2 with the value 'Apple'.
  2. We use the localeCompare() method with the arguments str2, 'en', and an options object with { sensitivity: 'base' } to compare str1 and str2 using English locale and base sensitivity.
  3. The result is stored in the variable comparison.
  4. We log comparison to the console using the console.log() method.

JavaScript Program

const str1 = 'apple';
const str2 = 'Apple';
const comparison = str1.localeCompare(str2, 'en', { sensitivity: 'base' });
console.log(comparison);

Output

0

Summary

In this JavaScript tutorial, we learned about localeCompare() method of String: the syntax and few working examples with output and detailed explanation for each example.