References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Strict Equality === operator

Operator JavaScript All modern browsers Updated
Quick answer

The strict equality operator === compares two values without converting their types — they must be the same type and the same value. 1 === 1 is true, but 1 === "1" is false. Always prefer it over loose ==, which coerces types and produces confusing results.

Overview

=== checks whether two values are strictly equal: same type and same value, with no type conversion. 5 === 5 is true; 5 === "5" is false because one is a number and the other a string. This predictability is exactly why it's the equality check you should reach for by default.

Its counterpart, loose equality ==, first coerces the operands to a common type and then compares — which leads to a famous gallery of surprises: 0 == "", 0 == false and null == undefined are all true, while "" == false is too. These conversions are hard to remember and easy to get wrong, so the widely accepted rule is: use === (and !==) everywhere. The one common, deliberate exception is x == null, a handy shorthand that matches both null and undefined.

Two specifics worth knowing. NaN === NaN is falseNaN is never equal to anything, including itself, so test with Number.isNaN(). And objects compare by reference, not contents: {a: 1} === {a: 1} is false because they're two different objects. To compare object contents you compare their fields, or stringify them.

Syntax

a === b   // strict: same type AND value
a !== b   // strict not-equal

5 === 5        // true
5 === "5"      // false (different types)
NaN === NaN    // false (use Number.isNaN)
{} === {}      // false (different references)

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const tests = [
    ['1 === 1',   1 === 1],
    ['1 === "1"', 1 === '1'],
    ['0 == ""',   0 == ''],
    ['0 === ""',  0 === '']
  ];

  document.getElementById('out').textContent =
    tests.map(t => t[0] + '  ->  ' + t[1]).join('\n');
</script>

Best practices

  • Use === and !== by default — avoid loose == and its coercion surprises.
  • The one common exception is x == null to check for null or undefined together.
  • Test for NaN with Number.isNaN(x), since NaN === NaN is false.
  • Remember objects compare by reference; compare their properties to check contents.

Frequently asked questions

What is the difference between == and === in JavaScript?
=== compares value and type with no conversion; == converts types first, which causes confusing results. Use ===.
Why is 1 === "1" false?
Because === doesn't convert types, and a number is not the same type as a string. 1 == "1" (loose) would be true after coercion.
Why is NaN === NaN false?
NaN is defined as not equal to anything, including itself. Use Number.isNaN(value) to test for it.
Why are two identical objects not ===?
Objects compare by reference, not by contents. Two separate objects with the same properties are different references, so === is false.