-4

When you comparing object with another object with same property why it returns false?

For Example 
var person={
  age:30

}

var person2={
  age:40

}

console.log(person==person) or console.log(person===person)

it show's in console false why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Avdhut
  • 3
  • 4

2 Answers2

2

Objects are reference types, which means the equality operators operate on a reference to the object in memory, not to its contents.

In your particular case, you could serialize the object to a string and then check

const compareSerializableObjects = (a, b) =>
  JSON.stringify(a) === JSON.stringify(b)
Benny Powers
  • 5,398
  • 4
  • 32
  • 55
1

person === person will always return true as you are comparing the same reference, and if you are comparing person === person2 then it is a different refference which is false. Did you mean person.age === person2.age ?

Marek Panti
  • 116
  • 1
  • 9