JavaScript String localeCompare() Method
The JavaScript String localeCompare() method is used to compare two strings in the current locales that your browser is currently using. It returns a number that indicates whether this string comes before, after, or is the same as the given string in sort order.
The following is a list of statements that describe the number returned by this method −
- A positive number (+1) indicates that the given string occurs after the compare string.
- A negative number (-1), indicates that the given string occurs before the compare string.
- A zero (0) will be returned if both strings are equivalent to each other.
Syntax
Following is the syntax of JavaScript String localeCompare() method −
localeCompare(compareString, locales, options)
Parameters
This method accepts three parameters named 'compareString', 'locales', and 'options', which are described below −
- compareString − The string needs to be compared.
- locales (optional) − The locales of the current browser.
- options (optional) − An object adjusting the output format.
Return value
This method returns a number 0 if both strings are equivalent, -1 if the given string occurs before the compare string, or +1 if the given string occurs after the compare string.
Example 1
When no locales or options are specified, it performs a basic comparison based on the Unicode values of the characters in the strings. If the strings are equivalent, it returns 0; otherwise, it returns a positive or negative value indicating their order.
In the following program, we are using the JavaScript localeCompare() method to compare the string "Tutorialspoint" with the given string "Tutorialspoint".
<html>
<head>
<title>JavaScript String localeCompare() Method</title>
</head>
<body>
<script>
const str = "TutorialsPoint";
const compare_str = "TutorialsPoint";
document.write("Original string: ", str);
document.write("<br>Compare string: ", compare_str);
document.write("<br>The str.localeCompare(compare_str) method returns: ", str.localeCompare(compare_str));
</script>
</body>
</html>
Output
The above programs returns 0.
Original string: TutorialsPoint Compare string: TutorialsPoint The str.localeCompare(compare_str) method returns: 0
Example 2
If the given string occurs after the compare string, the localeCompare() method returns 1.
The following is another example of the JavaScript String localeCompare() method. In this example, we use this method to compare the string "hello" with the given string "HELLO" using the current locales "en".
<html>
<head>
<title>JavaScript String localeCompare() Method</title>
</head>
<body>
<script>
const str = "HELLO";
const compare_str = "hello";
const locale = "en";
document.write("Original string: ", str)
document.write("<br>Compare string: ", compare_str);
document.write("<br>Current locales: ", locale);
document.write("<br>The str.localeCompare(compare_str) method returns: ", str.localeCompare(compare_str, locale));
</script>
</body>
</html>
Output
After executing the above program, it will return 1.
Original string: HELLO Compare string: hello Current locales: en The str.localeCompare(compare_str) method returns: 1
Example 3
When we pass all three parameters compareString, locales, and options to the localeCompare() method, it compares the compareString with the given string using the current locales, and the output format will be adjusted based on the specified options.
<html>
<head>
<title>JavaScript String localeCompare() Method</title>
</head>
<body>
<script>
const str = "tutorialspoint";
const compare_str = "TUTORIALSPOINT";
const locale = "en";
const option = {sensitivity: 'base'};
document.write("Original string: ", str)
document.write("<br>Compare string: ", compare_str);
document.write("<br>Current locales: ", locale);
document.write("<br>Options: ", option.sensitivity);
document.write("<br>The str.localeCompare(compare_str) method returns: ", str.localeCompare(compare_str));
document.write("<br>The str.localeCompare(compare_str, locale, option) method returns: ", str.localeCompare(compare_str, locale, option));
</script>
</body>
</html>
Output
The above program produces the following output −
Original string: tutorialspoint Compare string: TUTORIALSPOINT Current locales: en Options: base The str.localeCompare(compare_str) method returns: -1 The str.localeCompare(compare_str, locale, option) method returns: 0