Strict equality

2 snippets in JavaScript

JSJavaScript

Equality: == vs === vs Object.is

JS · Data Types
Syntax
a === b   // strict (no coercion)
a == b    // loose (with coercion)
Object.is(a, b)  // same-value equality
Example
console.log(0 == false);         // true
console.log(0 === false);        // false
console.log(NaN === NaN);        // false
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0));    // false

Note Always use === for comparisons. Object.is() handles edge cases like NaN and -0 that even === gets wrong.

== vs === Confusion

JS · Common Mistakes
Syntax
// Wrong: == with type coercion
// Right: === for strict comparison
Example
// These all evaluate to true (unexpected!)
console.log("" == false);    // true
console.log(0 == "");        // true
console.log(null == undefined); // true
console.log("0" == false);   // true

// Use strict equality
console.log("" === false);   // false
console.log(0 === "");       // false
console.log(null === undefined); // false

Note Rule of thumb: always use === and !==. The only acceptable == use is value == null to check both null and undefined at once.

Frequently asked questions

How does JavaScript handle strict equality?
JavaScript covers this with 2 copy-ready snippets on this page. The "Equality: == vs === vs Object.is" snippet in JavaScript uses `a === b // strict (no coercion)`.
Which code does the JavaScript example use?
The "Equality: == vs === vs Object.is" snippet uses `a === b // strict (no coercion)`, from the Data Types section of the JavaScript cheat sheet.
What other JavaScript snippets are shown for "strict equality"?
Besides "Equality: == vs === vs Object.is", this page also shows "== vs === Confusion".
Is there anything to watch out for?
Yes. For "Equality: == vs === vs Object.is": Always use === for comparisons. Object.is() handles edge cases like NaN and -0 that even === gets wrong.