Tutorials Point


  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Javascript String - localeCompare() Method


previous next AddThis Social Bookmark Button


Advertisements

Description:

This method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

Syntax:

string.localeCompare( param )

Here is the detail of parameters:

  • param : A string to be compared with string object.

Return Value:

  • 0 : It string matches 100%.

  • 1 : no match, and the parameter value comes before the string object's value in the locale sort order

  • -1 : no match, and the parameter value comes after the string object's value in the locale sort order

Example:

<html>
<head>
<title>JavaScript String localeCompare() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is beautiful string" );
var index = str1.localeCompare( "XYZ" );
document.write("localeCompare first :" + index ); 

document.write("<br />" ); 

var index = str1.localeCompare( "AbCD ?" );
document.write("localeCompare second :" + index ); 

</script>
</body>
</html>

This will produce following result:

localeCompare first :-1
localeCompare second :1  

To understand it in better way you can Try it yourself.



previous next Printer Friendly

Advertisements


  

Advertisements