Skip to content
Related Articles

Related Articles

Improve Article

TypeScript | String localeCompare() Method

  • Last Updated : 18 Jun, 2020

The localeCompare() is an inbuilt function in TypeScript which is used to get the number indicating whether a reference string comes before or after or is the same as the given string in sorted order. 
Syntax:

string.localeCompare( param ) 

Parameter: This method accept a single parameter as mentioned above and described. 

  • param : This parameter is a string to be compared with string object.

Return Value: This method returns the flowing. 

  • 0 is return, if the string is fully match.
  • 1 is return, if the string is not match and the parameter value comes before the string object’s value.
  • – A negative value is return, if the string is not match and the parameter value comes after the string object’s value.

Example 1: 

JavaScript






<script>
    // Original strings
    var str1 = new String('Geeksforgeeks'); 
  
    var index = str1.localeCompare("Geeksforgeeks");
  
    // Use of String localeCompare() Method
    console.log(index);
</script>

Output: 

0

Example 2: 

JavaScript




<script>
    // Original strings
    var str1 = new String('Geeksforgeeks'); 
  
    var index = str1.localeCompare("Geeks");
  
    // Use of String localeCompare() Method
    console.log(index);
</script>

Output: 

1
My Personal Notes arrow_drop_up
Recommended Articles
Page :