I would like to know why this happens and what is the primitive value of "a"?
a = [1,2,3]
b = "1,2,3"
a==b //this returns true
As far as I know when doing this the primitive type of a is pared with b.
EDIT:
According to the section "Loose equality using ==" When you pare an object (a) with a string (b) the following rule is valid:
ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.
Undestanding how "ToPrimitive(A)" works was crucial.
I would like to know why this happens and what is the primitive value of "a"?
a = [1,2,3]
b = "1,2,3"
a==b //this returns true
As far as I know when doing this the primitive type of a is pared with b.
EDIT:
According to the section "Loose equality using ==" When you pare an object (a) with a string (b) the following rule is valid:
ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.
Undestanding how "ToPrimitive(A)" works was crucial.
==
specifically ignores the type of the values it pares.
– JJJ
Commented
Sep 20, 2017 at 13:14
===
is safer and less random than ==
. But check Miniver Cheevy's link
– m.nachury
Commented
Sep 20, 2017 at 13:16
It returns true
because you are not using the strict parison operator ===
.
When using ==
you are actually doing [1,2,3].toString() === '1,2,3'
Equality (==) (source)
The equality operator converts the operands if they are not of the same type, then applies strict parison. If both operands are objects, then JavaScript pares internal references which are equal when operands refer to the same object in memory.
Identity / strict equality (===) (source)
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
console.log('[1,2,3] == "1,2,3"', [1,2,3] == "1,2,3");
console.log('[1,2,3] === "1,2,3"', [1,2,3] === "1,2,3");
console.log('[1,2,3].toString() === "1,2,3"', [1,2,3].toString() === "1,2,3");
The Abstract Equality Comparison Algorithm (source)
The parison x == y, where x and y are values, produces true or false. Such a parison is performed as follows:
1.If Type(x) is the same as Type(y), then
____a. If Type(x) is Undefined, return true.
____b. If Type(x) is Null, return true.
____c. If Type(x) is Number, then
________i If x is NaN, return false.
________ii If y is NaN, return false.
________iii If x is the same Number value as y, return true.
________iv If x is +0 and y is −0, return true.
________v If x is −0 and y is +0, return true.
________vi Return false.
____d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
____e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
____f Return true if x and y refer to the same object. Otherwise, return false.
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
4. If Type(x) is Number and Type(y) is String, return the result of the parison x == ToNumber(y).
5. If Type(x) is String and Type(y) is Number, return the result of the parison ToNumber(x) == y.
6. If Type(x) is Boolean, return the result of the parison ToNumber(x) == y.
7. If Type(y) is Boolean, return the result of the parison x == ToNumber(y).
8. If Type(x) is either String or Number and Type(y) is Object, return the result of the parison x == ToPrimitive(y).
9. If Type(x) is Object and Type(y) is either String or Number, return the result of the parison ToPrimitive(x) == y.
10. Return false.