JavaScript Course
JavaScript
/
Beginner

== vs ===

Definition

Double equals `==` (Loose Equality) compares two values after attempting to implicitly coerce them to the same type. Triple equals `===` (Strict Equality) compares both value AND type without doing any conversion.

Explain Like I'm New

Loose equality `==` is like a bartender who accepts a hand-drawn picture of an ID as proof of age. Strict equality `===` checks both the ID and scans it to make sure it's legally valid.

Real World Example

Always using `===` in React and modern JavaScript to prevent bizarre bugs where `0 == false` returns `true`.

Common Use Cases

  • •Comparing variables
  • •Linting rules (ESLint enforces `===`)

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Why should you almost always use `===` instead of `==`?

intermediate

  • Is there ever a good reason to use `==`?

advanced

  • What is `Object.is()` and how does it differ from `===`?

Flash Cards

Question

Is there ever a good reason to use ==?

Click to reveal answer
Answer

Rarely, but checking `if (val == null)` is a shorthand way to check if a value is EITHER `null` OR `undefined` simultaneously, because `null == undefined` is true.

Question

What is Object.is()?

Click to reveal answer
Answer

`Object.is()` is exactly like `===` except for two edge cases: it considers `NaN === NaN` to be true (unlike strict equality), and distinguishes between `-0` and `+0`.