String localeCompare() Method - Javascript String

Javascript examples for String:localeCompare


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



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>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

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;



Related Tutorials