- The localeCompare() method compares two strings in the current locale.
- The locale is based on the language settings of the browser.
- The localeCompare() method returns a number indicating whether the string comes before, after or is equal as the compareString in sort order.
- Code:-
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head>
- <body>
- <h1>localeCompare()</h1>
- <input type="button" onclick="strfun();" value="Get Answer">
- <script>
- function strfun()
- {
- var str1 = "xx";
- var str2 = "yy";
- alert(str2.localeCompare(str1)); //str2 smaller than str1 retun 1
- alert(str1.localeCompare(str2)); //str1 smaller then str2 retun -1
- alert(str1.localeCompare(str1)); //both are equal retun 0
- }
- </script>
- </body>
- </html>
What is localecompare JavaScript
Reviewed by Network security
on
June 28, 2019
Rating:
No comments: