Using String localeCompare() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string technique referred to as localeCompare() with syntax and examples.

Description

In JavaScript, localeCompare() is a string approach that is used to compare two strings and return a numeric cost indicating which string comes first in the type order primarily based on locale. Because the localeCompare() technique is a approach of the String object, it must be invoked through a specific instance of the String class.

Syntax

In JavaScript, the syntax for the localeCompare() method is:

string.localeCompare(compare_string [, locale [, options]]);

Parameters or Arguments

compare_string It is a string value that will be in contrast towards the localeCompare() method’s calling string. locale Optional. It is a BCP forty seven language tag or an array of such tags that will be used to decide the sort order. A BCP forty seven language tag defines a language that may additionally incorporate a fundamental language code as properly as an extension. If this parameter is not provided, the localeCompare() technique will use the host environment’s contemporary locale. options Optional. The preferences to follow to the type order. It can be one or extra of the following: Value Description caseFirst Determines how “case” will be treated when sorting. It can be one of the following values: upper – uppercase will be sorted first lower – lowercase will be sorted first false – use the locale’s default sorting (default) ignorePunctuation Determines whether punctuation will be ignored. It can be one of the following values: true – punctuation will be overlooked when sorting false – punctuation will now not be not noted when sorting (default) localeMatcher Determines the locale matching algorithm to use. It can be one of the following values: lookup best fit (default) numeric Determines whether or not numeric collation will be used. It can be one of the following values: true – numeric collation will be used when sorting false – numeric collation will no longer be used when sorting (default) sensitivity Determines whether numeric collation will be used. It can be one of the following values: base – strings that do not have the equal base letters are viewed unequal accent – strings that do no longer have the same base letters or accents are viewed unequal case – strings that do not have the identical base letters or case are considered unequal variant – strings that do not have the equal base letters, accents or case are considered unequal (default) usage Determines whether or not the technique will operate sorting or looking for matching string. It can be one of the following values: sort – technique will operate sorting (default) search – approach will search for matching strings

Returns

The localeCompare() method returns a bad range if the calling string appears before the compare_string in the type order.

The localeCompare() method returns a superb quantity if the calling string seems after the compare_string in the kind order.

The localeCompare() approach returns 0 if the calling string is equivalent to the compare_string and therefore seems at the identical function in the type order.

Note

The localeCompare() technique does no longer trade the value of the original string. Depending on the browser, the localeCompare() method may additionally return special tremendous or bad values when the strings are now not equivalent, such as 1, 2, -1, or -2. So when using this method, be certain to test for a high quality or poor value and not a specific numeric value such as -1. It is k to test for a cost of 0 when the calling string and compare_string are equivalent. This return cost does not differ between browsers.

Example

Let’s take a seem to be at an example of how to use the localeCompare() approach in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.localeCompare('abc'));

In this example, we have declared a variable known as totn_string that is assigned the string value of ‘TechOnTheNet’. We have then invoked the localeCompare() method of the totn_string variable to examine this string fee to ‘abc’.

We have written the output of the localeCompare() method to the net browser console log, for demonstration purposes, to exhibit what the localeCompare() technique returns.

The following will be output to the net browser console log:

1

In this example, the localeCompare() approach lower back a high-quality cost of 1 due to the fact the string ‘TechOnTheNet’ seemed after ‘abc’ in the kind order.

Depending on the browser, the localeCompare() technique may additionally return a distinct tremendous value such as 1 or 2, or any other positive value. Don’t count on that it will be a return price of 1.

Specifying a Locale Parameter

You can additionally supply a BCP forty seven language tag for the locale parameter to change the type order primarily based on locale.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.localeCompare('xyz','en-US'));

The following will be output to the net browser console log:

-1

In this example, the localeCompare() approach used a parameter of ‘en-US’ that sorted the strings the usage of US English. The method lower back a terrible price of -1 because the string ‘TechOnTheNet’ seemed earlier than ‘xyz’ the usage of this kind order.

Again, be aware that relying on the browser, the localeCompare() technique may additionally return a different negative value such as -1 or -2, or any different negative value. Don’t assume that it will be a return value of -1.

Specifying an Option Parameter

Next, the localeCompare() approach has many selections that you can specify that impacts the sort order.

For example:

var totn_string = '8';

console.log(totn_string.localeCompare('30'));

The following will be output to the web browser console log:

1

In this example, the localeCompare() technique returned 1 because the string cost ‘8’ comes after ’30’ using a string sort.

However, what if we favor to sort our values numerically? We can follow a numeric kind using an choice parameter as follows:

var totn_string = '8';

console.log(totn_string.localeCompare('30', 'en-US', {numeric:"true"}));

The following will be output to the net browser console log:

-1

Now the localeCompare() method again -1 due to the fact the value ‘8’ comes earlier than ’30’ when sorting the values numerically.