Skip to the solution
EasyFrontend5 min read

What is the difference between == and ===?

160 views

The problem

Question presented to candidate: "What's the difference between == and === in JavaScript, and why do most style guides tell you to always use ===?"

What a strong answer should cover:

  • πŸ“Œ Interview term: loose equality (==) β€” compares two values after converting them to a common type when their types differ. Verified directly: 1 == "1" is true because the string is coerced to a number first.
  • πŸ“Œ Interview term: strict equality (===) β€” compares both type and value, with no coercion at all. Verified: 1 === "1" is false because a number and a string are never equal under ===, regardless of their values.
  • A precise answer names the coercion rules that make == unpredictable: null == undefined is true (a special-cased pair β€” they equal each other and nothing else under ==), 0 == false is true, and "" == false is true β€” verified directly.
  • πŸ“Œ Interview term: the classic gotcha β€” [] == ![] evaluates to true. Verified by tracing it step by step: ![] evaluates first (an empty array is truthy, so ! of it is false), leaving [] == false, which coerces the array to a primitive ("") and then to a number (0), matching false's own 0.
  • NaN == NaN is false under both operators β€” coercion never makes NaN equal to anything, including itself β€” verified directly.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; the strong move is naming the coercion mechanism precisely rather than just "== is looser."

Code / implementation expected: Optional β€” walking through 2-3 concrete coercion examples (including the null == undefined special case) demonstrates real understanding better than reciting the rule from memory.

  • equality

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

5 min read Β· 8 sections

Run the code

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

JavaScriptReal coercion cases for == vs ===, including the [] == ![] gotcha traced step by step

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 40 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

How does `try...catch` work in JavaScript?

Continue