localeCompare()
compares two strings and returns:
localeCompare() |
Yes | Yes | Yes | Yes | Yes |
string.localeCompare(anotherString);
Parameter | Description |
---|---|
anotherString | Required. The string to compare |
Returns one of three values:
var stringValue = "A";
console.log(stringValue.localeCompare("a")); //-32
console.log(stringValue.localeCompare("A")); //0
console.log(stringValue.localeCompare("B")); //-1
/*from ww w . j a v a 2 s .co m*/
function determineOrder(value) {
var result = stringValue.localeCompare(value);
if (result < 0){
console.log("before");
} else if (result > 0) {
console.log("after");
} else {
console.log("equal");
}
}
determineOrder("a");
determineOrder("A");
determineOrder("B");
The code above generates the following result.