JavaScript String method: localeCompare()
[this page | pdf | back links]
The localeCompare() method (when applied
to a JavaScript
string) number
which is -1 if string is before specified (compare) string in ascending sort
order, 0 if they are the same and +1 if string is after specified string in
ascending sort order. It is case-sensitive.
It
has the following syntax with the following parameters:
string.localeCompare(comparestring)
Parameter
|
Required / Optional
|
Description
|
comparestring
|
Required
|
String to be searched
for
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
var y = "abcde", y2 = "cde";
document.getElementById("Example").innerHTML =
'var y = "' + y + '";<br>' +
'var x = y.localeCompare("' + y2 + '");';
document.getElementById("Result").innerHTML =
y.localeCompare(y2);
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodStringLocaleCompare() {
var z = "abc"; return !!z.localeCompare;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables