2

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.

Thanks

6
  • Please note, I'm voting to move this to SO. This isn't really a P.SE question. But, I think it fits on SO. Hence, I'm also giving a quick answer ... Commented Jun 28, 2016 at 17:40
  • @svidgen you might be interested in recent meta discussion: Should I answer a question which belongs on Stack Overflow? Commented Jun 28, 2016 at 18:01
  • 1
    @gnat Indeed. Didn't realize there was a consensus on this particular situation! Commented Jun 28, 2016 at 18:54
  • Sorry. About post to wrong site. My brain always goes code question = programmers, generic tech question = stackexchange. Commented Jun 28, 2016 at 18:56
  • 1
    Oh, not being snide or anything, but for the record I did look for what kind of question to post, and found this: "Programmers Stack Exchange ... who are interested in getting expert answers on conceptual questions about software development." off the Tour. I figured the question would fall under conceptual there because I was trying to remember the concepts behind why the code wasn't resolving to true. I thought there used to be a box that went straight to a detailed breakdown of what to post, but I don't see it anymore. Commented Jun 28, 2016 at 19:31

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's right! I totally forgot == and === only compare values. The references would be different so it would resolve as false. Thanks. It was irritating the heck out of me and when I googled around I couldn't find a good example similar (because I wasn't sure quite what to search for.)
1

It's a different object. The string representation is the same though so you can use https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify to convert the unique objects into a string. Then compare the strings and they will match.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.