0

I saw interview question here in debugging section (first question).

function greet(person) {
  if (person == { name: 'amy' }) {
    return 'hey amy'
  } else {
    return 'hey arnold'
  }
}
greet({ name: 'amy' })

This is printing hey arnold.

I want to understand why this is happening? Any explanation or direction to resource would be much appreciated.

Nirav
  • 602
  • 1
  • 10
  • 28
  • == will just do referential comparison. As these 2 are different objects, it won't be true. If you want to learn how to do deep comparison of objects refer to this post https://stackoverflow.com/questions/1068834/object-comparison-in-javascript – Mahendra Pratap Aug 26 '19 at 19:20
  • Thank You @MahendraPratap for the link. I've better understanding now. – Nirav Aug 26 '19 at 19:27

2 Answers2

1

objects are checked for equality by reference, not value.

{a: 1} === {a: 1} // returns false
David Ko
  • 304
  • 1
  • 3
  • 11
1

Objects are compared by reference, not by value. Two objects with the same properties and values are still different objects.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067