Strict equality (===)
The strict equality (===) operator checks whether its two operands are
equal, returning a Boolean result. Unlike the equality operator,
the strict equality operator always considers operands of different types to be
different.
Try it
Syntax
js
x === y
Description
The strict equality operators (=== and !==) provide the IsStrictlyEqual semantic.
- If the operands are of different types, return
false. -
If both operands are objects, return
trueonly if they refer to the same object. -
If both operands are
nullor both operands areundefined, returntrue. - If either operand is
NaN, returnfalse. - Otherwise, compare the two operand's values:
-
Numbers must have the same numeric values.
+0and-0are considered to be the same value. - Strings must have the same characters in the same order.
- Booleans must be both
trueor bothfalse.
-
Numbers must have the same numeric values.
The most notable difference between this operator and the equality
(==) operator is that if the operands are of different types, the
== operator attempts to convert them to the same type before comparing.
Examples
Comparing operands of the same type
js
"hello" === "hello"; // true
"hello" === "hola"; // false
3 === 3; // true
3 === 4; // false
true === true; // true
true === false; // false
null === null; // true
Comparing operands of different types
js
"3" === 3; // false
true === 1; // false
null === undefined; // false
3 === new Number(3); // false
Comparing objects
js
const object1 = {
key: "value",
};
const object2 = {
key: "value",
};
console.log(object1 === object2); // false
console.log(object1 === object1); // true
Specifications
| Specification |
|---|
| ECMAScript Language Specification # sec-equality-operators |
Browser compatibility
BCD tables only load in the browser