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:
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
.
✐ 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,
- We define two string variables,
str1
with the value'apple'
andstr2
with the value'banana'
. - We use the
localeCompare()
method to comparestr1
andstr2
. - The result is stored in the variable
comparison
. - We log
comparison
to the console using theconsole.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,
- We define two string variables,
str1
with the value'äpfel'
andstr2
with the value'banana'
. - We use the
localeCompare()
method with the argumentsstr2
and'de'
to comparestr1
andstr2
using German locale. - The result is stored in the variable
comparison
. - We log
comparison
to the console using theconsole.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,
- We define two string variables,
str1
with the value'apple'
andstr2
with the value'Apple'
. - We use the
localeCompare()
method with the argumentsstr2
,'en'
, and an options object with{ sensitivity: 'base' }
to comparestr1
andstr2
using English locale and base sensitivity. - The result is stored in the variable
comparison
. - We log
comparison
to the console using theconsole.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.