Computer >> Computer tutorials >  >> Programming >> Javascript

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


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 −

<!DOCTYPE html>
<html>
   <body>
      <script>
         var a = "klm";
         var b = "qrs";
         document.write("Character: "+a.localeCompare(b));
      </script>
   </body>
</html>