JavaScript String localeCompare() Method
You are Here:
JavaScript String localeCompare() Method
The localeCompare()
method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str1 ="ab";
var str2 ="ab";
var result = str1.localeCompare(str2);
document.write(result);
</script>
</body>
</html>
Syntax
str.localeCompare(compareString)
Parameter Values
Value | Type | Explanation |
---|---|---|
compareString | Required | The string to compare with. |
Return Values
Value | Explanation |
---|---|
-1 | If a string neither starts with nor equal to compareString. i.e., "cdab" = "ab" |
0 | If both strings are equal i.e., "ab" = "ab" |
1 | If a string starts with compareString. i.e., "abcd" = "ab". If both strings are equal, but are case sensitive. i.e., "AB" = "ab". |
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.