Javascript examples for String:localeCompare
The localeCompare() method compares two strings in the current locale.
reference.localeCompare(compareString);
Parameter | Description |
---|---|
compareString | Required. The string to compare with |
Value | Meaning |
---|---|
-1 | the reference string is sorted before the compareString |
0 | the two strings are equal |
1 | the reference string is sorted after the compareString |
The following code shows how to Compare two strings in the current locale:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from w w w. j ava 2 s .c om var str1 = "ab"; var str2 = "ab"; var n = str1.localeCompare(str2); document.getElementById("demo").innerHTML = n; } </script> </body> </html> </html>