Skip to the solution
EasyPhone Screen7 min read

What is the difference between Object.is, ==, and ===?

522 views

The problem

Question presented to candidate: "What is the actual difference between Object.is(a, b), a == b, and a === b? Walk me through where all three agree, and the specific cases where they disagree."

What a strong answer should cover:

  • == performs type coercion before comparing; === and Object.is never coerce types, so 1 === "1" and Object.is(1, "1") are both false while 1 == "1" is true.
  • === and Object.is agree on almost everything, EXCEPT exactly two IEEE-754 floating point edge cases: NaN and negative zero.
  • NaN === NaN is false (the famous exception), but Object.is(NaN, NaN) is true -- Object.is exists partly to give a correct way to test NaN equality without that gotcha.
  • 0 === -0 is true, but Object.is(0, -0) is false -- Object.is is actually STRICTER than === in this one specific case, not simply a safer alias for it.
  • None of the three perform structural/deep equality on objects or arrays -- two separately created array literals with identical contents are never equal under any of the three, since object comparison is always by reference.

Clarifying questions expected:

  • "Should I also cover Array.prototype.includes, since it uses a related but different algorithm called SameValueZero?"

Code / implementation expected: Yes -- a runnable truth table covering NaN, -0, type coercion, and reference equality.

  • equality
  • object-is
  • coercion

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptFull equality truth table: ==, ===, Object.is, and Array.includes (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 22 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is short-circuit evaluation?

Continue