LocaleCompare-1

 

JavaScript localeCompare function returns a number and tell whether string comes before, after or is the same as the compareString in sort order and this returns one of three values: -1 if the reference string is sorted before the 2nd string 0 if the two strings are equal 1 if the reference string is sorted after the 2nd string See the code snippet:

 
 
 
<!DOCTYPE html> <html> <head> <title>Welcome to LearnKode - A code learning platform</title> </head> <body> <button onclick="compareStrings()">Compare the strings</button> <p id="sample"></p> <script> function compareStrings() { var str1 = "Michael"; var str2 = "Voughan"; var n = str1.localeCompare(str2); document.getElementById("sample").innerHTML = n; } </script> </body> </html>
Output