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.
There are 3 variations for the syntax of String.localeCompare() method. They are:
localeCompare(compareString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
compareString | required | The 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
.
localeCompare(compareString, locales)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
compareString | required | The string against which the reference string is compared. |
locales | optional | A 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
.
localeCompare(compareString, locales, options)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
compareString | required | The string against which the reference string is compared. |
locales | optional | A string with a BCP 47 language tag, or an array of such strings, to specify the locale to use for sorting. |
options | optional | An 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
.
In JavaScript, we can use the localeCompare()
method to compare two strings and determine their sort order.
For example,
str1
with the value 'apple'
and str2
with the value 'banana'
.localeCompare()
method to compare str1
and str2
.comparison
.comparison
to the console using the console.log()
method.const str1 = 'apple';
const str2 = 'banana';
const comparison = str1.localeCompare(str2);
console.log(comparison);
-1
In JavaScript, we can use the localeCompare()
method to compare two strings using locale-specific sort order.
For example,
str1
with the value 'äpfel'
and str2
with the value 'banana'
.localeCompare()
method with the arguments str2
and 'de'
to compare str1
and str2
using German locale.comparison
.comparison
to the console using the console.log()
method.const str1 = 'äpfel';
const str2 = 'banana';
const comparison = str1.localeCompare(str2, 'de');
console.log(comparison);
1
In JavaScript, we can use the localeCompare()
method to compare two strings using locale-specific sort order and comparison options.
For example,
str1
with the value 'apple'
and str2
with the value 'Apple'
.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.comparison
.comparison
to the console using the console.log()
method.const str1 = 'apple';
const str2 = 'Apple';
const comparison = str1.localeCompare(str2, 'en', { sensitivity: 'base' });
console.log(comparison);
0
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.