JavaScript String localeCompare() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The javascript localecompare() is a built-in method in javascript that is used for returning a number and that number indicates if the reference string is coming before, or after, or it will be in the sorted order of the given string. The method returns a negative, a positive, or a zero value according to the compared analysis. If the reference string comes before the compare string, it gives a negative and if the reference string comes after the compare string, it gives a positive number. If the reference string comes equivalent to the compare string, it gives a zero (0). This method can be also implied with that Intl.Collator API support. When this method is implemented with the Intl.Collator API, is known as the Intl.Collator.

Syntax of JavaScript String localeCompare() method

The syntax of the javascript localecompare() method is as follows:

Parameters of JavaScript String localeCompare() method

The parameters of the javascript localecompare() method are as follows: There are two parameters of the javascript localecompare() method. The first one is compareString and the second one is options.

  • compareString: The compareString refers to the string that we need to compare with the reference string. This parameter is a required parameter.
  • options: The options parameter is sued for customizing the behavior of the given function. This parameter allows the application to specify the language of which language formatting convention should we need to use.

Return Value of JavaScript String localeCompare() Method

The return value of the javascript localecompare() method is as follows: The javascript localecompare() method returns an integer value as the returned value.

  • Return value: -1, or +1, or 0

If the method returns-1 in the case where the reference string comes before the compareString. It returns +1 in the case of the reference string coming after the compareString. And it returns 0 if the reference string and compareString are equivalent.

Exceptions of JavaScript String localeCompare() Method

There are not many exceptions to the javascript localeCompare() method but sometime we may see some exceptions that we are going to discuss. As we know this method returns -1, +1, and 0 as rerun values according to the performed comparison between the referenceString and the compareString. But the output numbers are not same always. These numbers will be positive, negative, and zero, but they may vary with the 1,2, or any other number. Like there may be -2 in place of -1. But the sign convention will always be the same. It will be negative for negative, and positive for positive in every browser.

Example

In the above sections we have seen the various aspects of the javascript localecompare() method and now let us see a sort and simple example to understand the working of this method.

Output:

Explanation: In the above example, we simply created a variable result1 and passed the reference string c to it. Then we passed b as the compare string. After that, we used the javascript localecompare() method for the comparison of the reference string to the compare string and assigned the return value to result1. Since the reference string c comes after the compare string b, the method returns 1 as the return value.

What is JavaScript String localeCompare() Method?

Before going to the topic of what is javascript localecompare() method is, let us have a brief discussion over what are the methods in javascript. The methods in javascript are defined as the set of code that is used to perform some specific tasks. All the methods have some definitions that help in performing the task. These methods store some property values. Now coming to the javascript localecompare() method.

The javascript localecompare() method in javascript is used for making the comparison of the given two strings. The method is a built-in method in javascript that is used for returning a number and that number indicates the occurrence of the given string. The number depends on the occurrence like if the reference string is coming before the reference string, or after that, or it i also possible that it is exactly the same as the given string. The method returns a negative, a positive, or a zero value according to the compared analysis. If the reference string comes before the compare string, it gives a negative and if the reference string comes after the compare string, it gives a positive number If the reference string comes equivalent to the compare string, it gives a zero (0).

Talking about the parameters and the arguments of the javascript localecompare() method. There are three parameters of the javascript localecompare() method. These parameters are compareString, options, and locale. The compareString refers to the string that we need to compare with the reference string. This parameter is a required parameter. The options parameter is sued for customizing the behavior of the given function. This parameter allows the application to specify the language of which language formatting convention should we need to use. The locale is an optional parameter. It is an array of the BCP 47 language tag that is used for determining the sort order. This language defines a language that contains two things that are a primary language code and an extension.

More Examples

Now let us see some more examples to understand the javascript localecompare() method in a better way. In the given section, various scenarios are given and explained with output and explanation where ever necessary.

  • Example 1. In this example, we have used the javascript localecompare() method for simply doing a comparison of strings using the localeCompare() method.

Output:

Explanation: In the above example, we created a variable result1 and passed the reference string b to it. Then we passed c as the compare string. After that, we used the javascript localecompare() method for the comparison of the reference string to the compare string and assigned the return value to result1. Since the reference string b comes before the compare string c, the method returns -1 as the return value.

  • Example 2. In this example, we have used the javascript localecompare() method for doing a comparison between the equal strings.

Output:

Explanation: In the above example, we created two variables result1 and result2. Then we passed two unequal strings to the variables. In result1, the HTML is the reference string and in result2, the CSS is the reference string. Then we passed HTML as compare string in both cases. In result1, both the strings are equal so the return value is 0. And in result2, the reference string (CSS) comes after the compare string (HTML) so the method returns 1.

  • Example 3. In this example, we have to use the javascript localecompare() method for doing a comparison with locales and options.

Output:

Explanation: In the above examples, we have used the localecompare() method with different locales and options for performing the comparison between the different strings.

Supported Browsers

The javascript localecompare() method is supported by almost all modern browsers. Given below are the name of some popular browsers that supports the javascript localecompare() method.

Browser NameSupported on versions
Mozilla Firefox1.0 and above
Microsoft Edge12.0 and above
Google Chrome1.0 and above
Safari3.0 and above
Opera7.0 and above

Conclusion

  • The javascript localecompare() method in javascript is used for making the comparison of the given two strings. The method returns a negative, a positive, or a zero value according to the compared analysis.
  • If the reference string comes before the compare string, it gives a negative and if the reference string comes after the compare string, it gives a positive number and If the reference string comes equivalent to the compare string, it gives a zero (0).
  • There are two parameters of the javascript localecompare() method. The first one is compareStringand the second one isoptions`.
  • The compareString refers to the string that we need to compare with the reference string. This parameter is a required parameter and the options parameter is used for customizing the behavior of the given function.
  • The javascript localecompare() method returns an integer ( -1 / +1 / 0) value as the returned value.
  • The name of some popular browsers that supports the javascript localecompare() methods are Mozilla Firefox, Google Chrome, Microsoft Edge, Safari Browser, and Opera Browser.