String localeCompare() Method - Javascript String

Javascript examples for String:localeCompare

Description

The localeCompare() method compares two strings in the current locale.

Syntax

reference.localeCompare(compareString);

Parameter Values

Parameter Description
compareString Required. The string to compare with

Return Value:

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:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials