Why does this evaluate to false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does this evaluate to false?

var x ={age:24}; var y = x; var z ={age:24}; alert(y==(x==z)); Can’t work it out! So if I ran this as separate statements. And y==x evaluates to true, and z==x evaluates to false. But I can’t figure out why! Any help appreciated please! Thx in advance peeps :-)

10th Mar 2019, 7:48 AM
Dan Leighton
Dan Leighton - avatar
2 Answers
+ 7
Objects in js are comparated by reference, then, them are equals ONLY if either refer to same object (eg. dont matter if either have same key/value pairs) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
10th Mar 2019, 8:21 AM
KrOW
KrOW - avatar
+ 1
Ah! fantastically useful advice!
10th Mar 2019, 12:56 PM
Dan Leighton
Dan Leighton - avatar