I've been playing around with arrays in JavaScript and cannot figure out why this happens:
console.log(0 == 0)
//true
console.log([] == 0)
//true
console.log(0 == [])
//true
console.log([] == [])
//false
console.log([] == ![])
// true
I've been playing around with arrays in JavaScript and cannot figure out why this happens:
console.log(0 == 0)
//true
console.log([] == 0)
//true
console.log(0 == [])
//true
console.log([] == [])
//false
console.log([] == ![])
// true
The empty array is equal enough to zero both left and right, but why isn't it equal to itself?
I realise that paring two objects would not result true
, but why are they coerced to 0
(or falsy, which shouldn't be the case) if you pare them to 0
, while threated as an object if you pare them to the other array?
- 1 stackoverflow./questions/30820611/… – sinisake Commented Dec 28, 2016 at 11:14
- 1 Possible duplicate of Why are two identical objects not equal to each other? – baao Commented Dec 28, 2016 at 11:16
-
@sinisake That would explain why they don't match each other, but the fact they match
0
is confusing me pletely – Randy Commented Dec 28, 2016 at 11:16 -
1
but
console.log([] === 0)
givesfalse
– RomanPerekhrest Commented Dec 28, 2016 at 11:17 - 1 JS is a very loosely typed language and polymorphism runs through the veins of it. So i would advice you to study type coercion in JS topic. – Redu Commented Dec 28, 2016 at 11:29
4 Answers
Reset to default 8console.log(0 == [])
//true
You are trying to pare object with an integer, so your object is implicitly typecasted to equivalent integer value that is 0
console.log([] == [])
//false
as two objects are never equal
console.log([] == [])
That will pare whether array1 and array2 are the same array object in memory, which is not what you want.
In order to do what you want, you'll need to check whether the two arrays have the same length, and that each member in each index is identical.
console.log([].length == [].length)
// true
Since the plete answer is never given and I actually understand it now, I'll provide the answer myself.
I found this in the Ecma-262 pdf:
It basically reads that [] == 0
is the same as Number([]) == 0
which is the same as 0 == 0
which is true. This does not apply to strict ===
.
There is no rule to pare objects other then rule number one, which is x is the same as y
. This means the same in everything, also memory address. Since they are not sharing the same memory address, rule 10 applies (return false).
The parison
x == y
, wherex
andy
are values, produces true or false. Such a parison is performed as follows:
If
Type(x)
is the same asType(y)
, thena. Return the result of performing Strict Equality Comparison
x === y
.If
x
isnull
andy
isundefined
, return true.- If
x
is undefined
andy
isnull
, return true.- If
Type(x)
isNumber
andType(y)
isString
, return the result of the parisonx == ToNumber(y)
.- If
Type(x)
isString
andType(y)
isNumber
, return the result of the parisonToNumber(x) == y
.- If
Type(x)
isBoolean
, return the result of the parisonToNumber(x) == y
.- If
Type(y)
isBoolean
, return the result of the parisonx == ToNumber(y)
.- If
Type(x)
is eitherString
,Number
, orSymbol
andType(y)
isObject
, return the result of the parisonx == ToPrimitive(y)
.- If
Type(x)
isObject
andType(y)
is eitherString
,Number
, orSymbol
, return the result of the parisonToPrimitive(x) == y
.- Return false
This question is handled with the knowledge of object reference and type conversion more properly.First,in javascript, object value is stored by reference.So we can tell it is different from [] and [],because the two array correspond with two different addr in memory.Second, '==' is a not rigorous operation for both left and right, [] and 0 are both transformed to false
.