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
Updated on 23-Jun-2020 07:47:07

Advertisements