LearnThatStack Ace your next interview
JavaScript · question
Question 8 of 126

Explain the difference between == and ===

beginner
← All JavaScript questions
Re-explain

=== compares value and type with no conversion, while == converts both sides to a common type and then compares. That conversion is called type coercion.

With loose equality, 5 == "5" is true because the string becomes a number first. With strict equality, 5 === "5" is false because a number and a string are different types. A special case is null == undefined, which is true, while null === undefined is false.

The coercion rules are hard to predict, so use === by default. The one common exception is value == null, which checks for null and undefined together.

5 == "5";    // true (coercion)
5 === "5";   // false (different types)
null == undefined;  // true
null === undefined; // false
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What conversion steps does == apply when one side is a boolean or an object? Booleans become numbers, objects go through ToPrimitive, then the comparison runs again. Show why "0" == false is true.
  • Why is NaN === NaN false, and what's the right way to test for NaN? NaN is unequal to everything, itself included. Use Number.isNaN or Object.is. The global isNaN converts to a number first, so isNaN("abc") is true.
  • How is Object.is different from ===? They differ in two cases only. Object.is(NaN, NaN) is true and Object.is(0, -0) is false.
  • Two objects have identical properties. Why does === return false, and how would you compare them? For objects, === compares references. Contrast a shallow key-by-key check with a recursive deep comparison.
  • Is there ever a good reason to write == in production code? value == null is the accepted case. Say the team rule allows only that and uses === everywhere else.

What you can say

  1. Triple equals checks value and type with no conversion, while double equals converts both sides to a common type and then compares.
  2. That conversion is called type coercion. It's why 5 == "5" is true, because the string turns into the number 5 first.
  3. With triple equals, 5 === "5" is false, because a number and a string are different types.
  4. One special case is null == undefined, which is true, while null === undefined is false.
  5. The coercion rules are hard to predict, so I use triple equals by default.
  6. The one exception I'd make is value == null, which checks for null and undefined in a single comparison.

Weak answers to avoid

  • Says == ignores type, never names coercion == doesn't ignore type. It converts the values first. Name type coercion and use 5 == "5" to show the mechanism.
  • Claims === compares object contents For objects, both operators compare references, so {} === {} is false. Miss this and the interviewer will doubt how well you know reference types.
  • Bans == outright with no exception It sounds like a linter rule repeated from memory. Mention value == null as the one common deliberate use and say what it catches.
  • Recites [] == ![] tricks without the rule Party tricks show memory, not understanding. Explain the conversion rule and why it makes === the safer default.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime