Strict equality (===)
Compares values without type conversion.
Available since: ES3
Syntax
a === b / a !== bExamples
Strict and loose comparison:
console.log(5 === 5);
console.log(5 === '5');
console.log(5 == '5');Output:
true
false
trueOddities of loose comparison:
console.log(0 == '');
console.log(null == undefined);
console.log(null === undefined);Output:
true
true
falseObjects are compared by reference:
const a = { x: 1 };
const b = { x: 1 };
console.log(a === b);
console.log(a === a);Output:
false
trueNotes
Always use === and !==. Loose == converts types by confusing rules first. The only common exception is value == null, which checks for both null and undefined.
See also
- Object.is — Compares two values almost like === but handles NaN and -0 correctly.
- typeof — Returns a string with the type of a value.
- Number.isNaN — Checks that a value is NaN, without type conversion.
In the tutorial
- Comparison — Comparison operators in JavaScript — greater, less, strict and loose equality, comparing strings and NaN.