Brian Chandler Brian Chandler - 1 year ago 110
Javascript Question

Javascript: Comparing two identical object literals

I'm pretty sure I figured this out before, but can't remember why this resolves to false. Can someone explain why?

if ({foo: 'bar'} === {foo: 'bar'}) {
console.log("Strictly Equal");
} else {
console.log("Not Equal");

I also tried == but it still has the two object literals as not equal.


Answer Source

== and === don't perform deep comparisons. They'll perform value comparisons for value types. But, objects are compared as references -- and those aren't the same object, they simply happen to be identical.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download