String localeCompare() Method
localeCompare() compares two strings and returns:
- If the string is alphabetically before the string argument, a negative number is returned.
- If the strings are equal, 0 is returned.
- If the string is alphabetically after the string argument, a positive number is returned.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var stringValue = "A";
document.writeln(stringValue.localeCompare("a")); //-32
document.writeln(stringValue.localeCompare("A")); //0
document.writeln(stringValue.localeCompare("B")); //-1
function determineOrder(value) {
var result = stringValue.localeCompare(value);
if (result < 0){
document.writeln("before");
} else if (result > 0) {
document.writeln("after");
} else {
document.writeln("equal");
}
}
determineOrder("a");
determineOrder("A");
determineOrder("B");
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
String:
- The String Type
- String length property
- String charAt()
- String charCodeAt()
- String concat()
- String slice()
- String substr()
- String substring()
- String indexOf()
- String lastIndexOf()
- String trim() Method
- String toLowerCase()
- String toLocaleLowerCase()
- String toUpperCase()
- String toLocaleUpperCase()
- String match()
- String search()
- String replace()
- String split()
- String localeCompare() Method
- String fromCharCode() Method
- String HTML Methods