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.


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>



Parameter Values

compareStringRequiredThe string to compare with.

Return Values

-1If a string neither starts with nor equal to compareString.
i.e., "cdab" = "ab"
0If both strings are equal
i.e., "ab" = "ab"
1If a string starts with compareString.
i.e., "abcd" = "ab".
If both strings are equal, but are case sensitive.
i.e., "AB" = "ab".


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.

Share this Page

Meet the Author