What are the pros and cons of each of them when pared to each other?
In which cases should ==
be used over localeCompare
and viceversa, in Javascript?
What are the pros and cons of each of them when pared to each other?
In which cases should ==
be used over localeCompare
and viceversa, in Javascript?
- developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Ali Habibzadeh Commented Jan 21, 2015 at 10:39
- 1 conceptf1.blogspot.co.uk/2014/01/… – Ali Habibzadeh Commented Jan 21, 2015 at 10:39
4 Answers
Reset to default 3They are not at all the same!
Non-strict parison pares 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 es 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 es before c
'a'.localeCompare('a') // returns 0, they are the same
'c'.localeCompare('a') // returns a positinve value, i.e. 1 etc, c es 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
localeCompare
doesn't just check for equality in value. It also pares 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.
localeCompare
is not the same as ==
. When you pare 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 es 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 bee handy when you want to see what variable es first in sort order, however it can be used to pare 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
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.