in js, falsy values are 0, null, undefined, false, NaN and '' and everything else is truthy.
So, that means [] and {} are also truthy values but when comparing []=={}
or []==[]
or {}=={}
are return false, why is that?

- 305
- 3
- 13
-
Because they do not reference the same things. – Adam Jenkins Apr 12 '18 at 15:52
-
reference same things mean? – Pranab Apr 12 '18 at 15:53
-
@Pranab — They are two identical arrays, not the same array. Object comparison asks if they are the same object, not if they are identical. – Quentin Apr 12 '18 at 15:54
-
The fact that `[]` (or `{}`) is a truthy value is only relevant in a boolean context, when `[]` has to be coerced to a boolean value. The comparison `[]==[]` evaluates to a boolean value, but isn't a boolean context for each `[]`. – Ted Hopp Apr 12 '18 at 15:54
2 Answers
If two things are "truthy", it doesn't mean that they are "equal" or "the same".
For example, 5
and 6
are "truthy", and yet you wouldn't be surprised at finding out that (5 == 6) === false
, would you?
The expression []
in JavaScript means "create a new array". If you do that two times in a row, that would mean "create one array, then create another array". If you then compare those two arrays, why would you expect them to be the same?
Similarly, the expression {}
means "create a new object", so if you do that twice, that's two different objects.
A deeper answer would be this: JavaScript comparison is "shallow". This means that it doesn't go "inside" the values to compare their innards, it only compares what's at the surface.
For primitive types, such as numbers or strings, comparing "at the surface" means "compare by value". But for more complex values, such as arrays or objects, comparing "at the surface" means "compare by reference". This is why two different, though apparently identical, objects, are not "equal" in the sense of comparison operators ==
and ===
.
Some languages, such as C# or Haskell, allow defining (sometimes automatically) custom meanings of "equality". Other languages, such as F# or Ocaml, even provide what's called "structural equality" (i.e. comparing all innards, to the bottom) by default for all types. JavaScript is not one of such languages.

- 78,590
- 9
- 125
- 172
-
yes, 5==6 is false cause their values different but here inside array or objects there are no values so how did they return false? – Pranab Apr 12 '18 at 15:55
-
3@Pranab - because object references compared with `==` returns `true` only when they refer to the same object. – Ted Hopp Apr 12 '18 at 15:56
-
-
@Pranab - this might help, comparing arrays doesn't check to see if the contents of the array are the same, it checks to see if the arrays are the same. Note: `[1,2] == [1,2]` is also false because it checks to see if the arrays are the same JavaScript object, not if they contain the same things. – Adam Jenkins Apr 12 '18 at 15:58
-
thanks, I got it... so, what about []==true, they also return false. means if compare anything with [] or {}, then every time I'll get false, right? – Pranab Apr 12 '18 at 16:10
-
@Pranab almost. `let x = [], y = x; x == y;` If you compare the exact same object referenced to itself, then you'll get true. – Adam Jenkins Apr 12 '18 at 16:14
-
thanks, @Adam, think I got the idea and one more thing, when compare Boolean with empty array or object, they return also false. like true=={} or true==[]? – Pranab Apr 12 '18 at 16:41
-
@Pranab That would be because an empty array is not a Boolean value. Why would you expect them to be equal? – Fyodor Soikin Apr 12 '18 at 16:41
-
ah ok. these comparisons really confuse me a lot. always truthy and falsy values come to my mind when I see any comparison. – Pranab Apr 12 '18 at 16:44
When you type []
you are effectively writing new Array()
(ditto with {}
being new Object
).
So look at it this way:
let x = new Array();
let y = new Array();
You would not expect x == y
to be true because they reference two different arrays.

- 51,445
- 11
- 72
- 100