-1

i have two different object with same values
for example :

obj = {name:"John"}
obj 2 = {name :"John"}

when i am trying compare both object, not values its giving me false

obj === obj 2 or obj == obj 2       //i tried both

i thought i will return true , but i don't know why it return false even both values are same ;

And when i did this

obj 3 = obj
obj 3 === obj

it returns me true as output.

help me. . Thanks in advance.

midnightgamer
  • 394
  • 1
  • 6
  • 16

1 Answers1

2

You can use JSON.stringify()

var obj = {name:"John"}
var obj2 = {name :"John"}
console.log(JSON.stringify(obj) == JSON.stringify(obj2));
console.log(JSON.stringify(obj) === JSON.stringify(obj2))
Mamun
  • 62,450
  • 9
  • 45
  • 52