What is localecompare JavaScript


  • 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:-

  1. <!DOCTYPE html> 
  2. <html> 
  3.  
  4. <head> 
  5.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  6. </head> 
  7.  
  8. <body> 
  9.  
  10.  <h1>localeCompare()</h1> 
  11.  
  12.  <input type="button" onclick="strfun();" value="Get Answer"> 
  13.  
  14.  <script> 
  15.   function strfun() 
  16.   { 
  17.    var str1 = "xx"; 
  18.    var str2 = "yy"; 
  19.  
  20.    alert(str2.localeCompare(str1)); //str2 smaller than str1 retun 1 
  21.    alert(str1.localeCompare(str2)); //str1 smaller then str2 retun -1 
  22.    alert(str1.localeCompare(str1)); //both are equal retun 0 
  23.   } 
  24.  </script> 
  25.  
  26. </body> 
  27.  
  28. </html>
What is localecompare JavaScript What is  localecompare JavaScript Reviewed by Network security on June 28, 2019 Rating: 5

No comments:

Useful Information

Powered by Blogger.