14

Why {} == {} and {} === {} is false in javascript?

{} == {} // false
{} === {} // false
rel1x
  • 2,351
  • 4
  • 34
  • 62

2 Answers2

15

1) The reason for this is that internally JavaScript actually has two different approaches for testing equality. Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference. That comparison by reference basically checks to see if the objects given refer to the same location in memory.so

{} == {}   is false

2) it does not make any difference whether you use == or === for comparing objects, because comparing them always returns false.

BIdesi
  • 341
  • 2
  • 12
12

javascript compares objects by identity, not value. Each object, each {} is distinct.

Same applies to arrays, too.

Andras
  • 2,995
  • 11
  • 17