Aquarius_Girl
Aquarius_Girl

Reputation: 22926

In which cases should `==` be used over `localeCompare` and viceversa, in Javascript?

What are the pros and cons of each of them when compared to each other?
In which cases should == be used over localeCompare and viceversa, in Javascript?

Upvotes: 3

Views: 3187

Answers (4)

adeneo
adeneo

Reputation: 318252

They are not at all the same!

Non-strict comparison compares strings to see if they are the same (only included strings as that's what localeCompare works on, types are irrelevant).

"test" == "test" // true

localCompare is a lot more than that, it returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order, and in the specified language.

'a'.localeCompare('c') // returns a negative value, i.e. -1 etc, a comes before c
'a'.localeCompare('a') // returns 0, they are the same
'c'.localeCompare('a') // returns a positinve value, i.e. 1 etc, c comes after a

note that the sort order used is entirely implementation dependent, but in most browsers it will be alphabetical

or language specific

'ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z

As it return a negative integer, zero or a positive integer, it's useful in functions such as sort() which expects the return of the sorting callback to be just that, a negative integer, zero or a positive integer.

MDN

Upvotes: 3

Mik378
Mik378

Reputation: 22181

localeCompare is very useful to implement a sorting (ascending or descending) function:

myLabelsArray.sort(function(a, b){
    return a.label.localeCompare(b.label);
});

Indeed, localeCompare returns -1, 0 (if equals) or 1 (based on locale language rules), allowing to get a sorting.

It would take more lines to implement it with == + <, explicitly returning integers.

Upvotes: 1

Kevin
Kevin

Reputation: 862

localeCompare is not the same as ==. When you compare two variables using ==, you check whether or not the variables have the same content. This will return a boolean (true/false). However, localeCompare does not return a boolean but an int instead.

You will receive a 0 when the two variables are the same, but you will however receive either 1 or -1 if your variables are not the same. The value is based on whether the first variable comes before or after the second variable in sort order.

So I myself would use == when I'm purely validating if two variables are the same, but localeCompare can become handy when you want to see what variable comes first in sort order, however it can be used to compare two variables to see if they are the same.

string a = "hello";
string b = "world";

a == b // returns false
a.localeCompare(b); // returns -1

To answer your question slightly, these are the characteristics / pro's and cons of using either of the examples given:

Using ==

  • Returns a boolean instead of a string/integer/...
  • Easier to read by most people

Using localeCompare

  • Returns an integer (-1, 0 or +1)
  • Can be used to sort variables

Upvotes: 1

Cerbrus
Cerbrus

Reputation: 72867

localeCompare doesn't just check for equality in value. It also compares the values when they're different, and returns a 1 / -1 depending on which value is higher. It's return value is in no way similar to a equality check.

localeCompare can be used for sorting strings, as the return value is different from == when the 2 values aren't the same, == can only be used for determining equality (in value, not type).

For example:

"2".localeCompare(2) // 0
"2".localeCompare(1) // 1
"2".localeCompare(3) // -1

"2" == 2 // true
"2" == 1 // false
"2" == 3 // false

"b".localeCompare("b") // 0
"b".localeCompare("a") // 1
"b".localeCompare("c") // -1

"b" == "b" // true
"b" == "a" // false
"b" == "c" // false

The return value from localeCompare happens to be exactly what Array.prototype.sort() expects to be returned from it's handler.

Upvotes: 2

Related Questions