W3cubDocs

/JavaScript

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.

Syntax

x === y

Description

The strict equality operators (=== and !==) use the Strict Equality Comparison Algorithm to compare two operands.

  • If the operands are of different types, return false.
  • If both operands are objects, return true only if they refer to the same object.
  • If both operands are null or both operands are undefined, return true.
  • If either operand is NaN, return false.
  • Otherwise, compare the two operand's values:
    • Numbers must have the same numeric values. +0 and -0 are considered to be the same value.
    • Strings must have the same characters in the same order.
    • Booleans must be both true or both false.

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

console.log("hello" === "hello");   // true
console.log("hello" === "hola");    // false

console.log(3 === 3);               // true
console.log(3 === 4);               // false

console.log(true === true);         // true
console.log(true === false);        // false

console.log(null === null);         // true

Comparing operands of different types

console.log("3" === 3);           // false

console.log(true === 1);          // false

console.log(null === undefined);  // false

Comparing objects

const object1 = {
  name: "hello"
}

const object2 = {
  name: "hello"
}

console.log(object1 === object2);  // false
console.log(object1 === object1);  // true

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Strict equality (a === b) 1 12 1 4 4 1
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Strict equality (a === b) 1 18 4 10.1 1 1.0
Server
Node.js
Strict equality (a === b) 0.1.100

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality