Sub topic 7
Strict Equality with ===
This chapter assumes you know JavaScript's primitive types (number, string, boolean, null, undefined, symbol, bigint), can tell primitives from objects, and can run code in a browser console or Node.js.
The Working Model
The strict equality operator === returns true when both operands have the same type and the same value. It never converts types. If the types differ, the answer is false and no further checks run. Its negation, !==, returns the opposite result.
Because === never converts its operands, you can predict its result by reading the code. That is why most style guides, and linters such as ESLint's eqeqeq rule, recommend === as the default comparison.
console.log(1 === 1); // true
console.log('abc' === 'abc'); // true
console.log(1 === '1'); // false: number vs string
console.log(true === 1); // false: boolean vs number
console.log(null === undefined); // false: different types
console.log(undefined === undefined); // true
console.log(1n === 1n); // true: BigInt compared by value
console.log(5 !== '5'); // trueHow the Comparison Proceeds
Each type follows its own rule:
| Type | Equal when |
|---|---|
| number | Same numeric value. `NaN` never matches, and `+0 === -0` is true |
| string | Same sequence of UTF-16 code units |
| boolean, null, undefined | Same value |
| bigint | Same mathematical value |
| symbol | The very same symbol |
| object | The very same object in memory |
Common Errors
Strict equality is simple, but some results still catch people.
- NaN is never equal to itself.
NaN === NaNisfalse. UseNumber.isNaN(x)to test for it. A later chapter coversNaNand-0in depth. - Symbols are unique.
Symbol('id') === Symbol('id')isfalse, because each call creates a new symbol. - Strings that look identical can differ. An accented letter can be stored as one code point or as a base letter plus a combining mark.
- Values read from input are strings. A form field holding
"42"fails=== 42. Convert it first withNumber(value).
const precomposed = 'caf\u00e9'; // é as one code point
const combined = 'cafe\u0301'; // e followed by a combining accent
console.log(precomposed === combined); // false
console.log(precomposed.normalize() === combined.normalize()); // true
const input = '42';
console.log(input === 42); // false
console.log(Number(input) === 42); // trueFor objects and arrays, === checks identity, not contents. A later chapter covers comparing objects in detail.
Exercise
Predict the output of each line: 0 === -0, '5' === 5, null === null, and Symbol.for('k') === Symbol.for('k').
Show answer
true, false, true, true. +0 and -0 are equal under ===, a string and a number differ in type, null equals itself, and Symbol.for returns the same symbol from the global registry for the same key.
Exercise
A function receives qty from a URL query string and checks if (qty === 0). The check never succeeds, even for ?qty=0. Why, and how do you fix it?
Show answer
Query string values are strings, so qty is '0' and '0' === 0 is false because the types differ. Convert it first: if (Number(qty) === 0).