How to compare two strings in the current locale with JavaScript?

HTMLProgramming ScriptsJavascript

To compare two strings in the current locale, use the localeCompare() method. It returns 0 if the strings are equal, -1 if string1 is sorted before string2, and 1 if string1 is sorted before string2.

Example

You can try to run the following code to learn how to compare two strings in the current locale in JavaScript −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var a = "klm";
         var b = "qrs";
         document.write("Character: "+a.localeCompare(b));
      </script>
   </body>
</html>
raja
Published on 26-Feb-2018 11:10:49
Advertisements