Open In App

TypeScript String localeCompare() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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

Last Updated : 03 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads