Friday, September 5, 2025
HomeLanguagesJavascriptTypeScript String localeCompare() Method

TypeScript String localeCompare() Method

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('Geeksforneveropen'); 
  
    var index = str1.localeCompare("Geeksforneveropen");
  
    // Use of String localeCompare() Method
    console.log(index);
</script>


Output: 

0

Example 2: 

JavaScript




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


Output: 

1
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS