-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
  • 444
  • 1
  • 5
  • 18

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
  • 66,969
  • 9
  • 47
  • 59