Home » Javascript Tutorial » Equality Operators in Javascript
JAvascript

Equality Operators in Javascript

Equality Operators in JavaScript

Equality operators in JavaScript are either strict or type–converting. Strict operators ( such as the strict equality, ===) compare both the type and the content of operants whilst the type converting operators (such as the abstract equality, ==) first convert the operands to the same type before comparing them.

Equality (==)

The equality operator compares the contents of the operants after type-converting them to the same data type. Here is wha you should know about the equality operator:

  • two strings are only equal if they have the same character sequence, same length, and same characters with the same case in corresponding positions.
  • two numbers are only equal if they are numerically equal.
  • NaN is not equal to anything even itself.
  • undefined is equal to undefined, which means two uninitialised variables are equal.
  • Negative zero is equal to positive zero.
  • boolean operands are only equal if both are true or both are false.
  • If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
  • null and undefined are equal to each other and to themselves.
  • Two distinct objects are never equal even if they have the same properties.
  • Objects are only equal if they reference the same Object.

Examples:

// comparing number types
console.log(1 == 0); // false
console.log(1 == 1); // true
console.log(23 == 87); //false

// comparing string types
console.log("JavaScipt" == "Java"); // false
console.log("hello" == "HELLO"); // false
console.log("script" == "script") // true

// comparing boolean types
console.log(true == true); // true
console.log(false == false); // true
console.log(true == false); // false
// comparison with NaN
console.log(1 == NaN); // false
console.log(NaN == true); // false
console.log("1" == NaN); // false
console.log(NaN == "0"); // false
console.log(NaN == undefined); // false
console.log(NaN == NaN); // false
console.log(NaN == null); // false
// undefined variables are equal
let x;
let y;

console.log(x == y); // true
// negative zero is equal to zero
console.log(-0 == 0); // true
console.log(-1 == 1); // false
// null and undefined
console.log(0 == null); // false
console.log(undefined == 0); // false
console.log(null == undefined); // true
console.log(null == null); // true
console.log(undefined == undefined); // true
// comparing objects
let rocket = {};
let spaceship = {};
let missile = rocket;

rocket.color = "blue";
spaceship.color = "blue";

console.log(rocket == spaceship); // false
console.log(missile == rocket); // true
// type conversion in the equality operator

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

Strict equality (===)

The identity operator (strict equality) returns true only if the operands are strictly equal (equal contents and same type).

Examples to show how the equality operator differs from the identity operator.

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

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

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

console.log(1 == "0"); // false
console.log(1 === "0"); // false

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

console.log("1" == false); // false
console.log("1" === false); // false
// comparing number types
console.log(1 === 0); // false
console.log(1 === 1); // true
console.log(23 === 87); //false

// comparing string types
console.log("JavaScipt" === "Java"); // false
console.log("hello" === "HELLO"); // false
console.log("script" === "script") // true

// comparing boolean types
console.log(true === true); // true
console.log(false === false); // true
console.log(true === false); // false
// null and undefined
console.log(0 == null); // false
console.log(0 === null); // false

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

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

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

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

Inequality (!=)

The inequality operator (!=) is the returns true if the operands are not equal. This operator is not strict, which means that if the two operands are not of the same data type, they are converted to the same type for the comparison. It works the same way as the equality (==) but in the opposite sense.

Examples:

// comparing number types
console.log(1 != 0); // true
console.log(1 != 1); // false
console.log(23 != 87); // true

// comparing string types
console.log("JavaScipt" != "Java"); // true
console.log("hello" != "HELLO"); // true
console.log("script" != "script") // false

// comparing boolean types
console.log(true != true); // false
console.log(false != false); // false
console.log(true != false); // true

Strict inequality (!==)

The non-identity (strict inequality) operator returns true for operants that are either not equal or not the same type.

// comparing number types
console.log(1 !== 0); // true
console.log(1 !== 1); // false
console.log(23 !== 87); // true

// comparing string types
console.log("JavaScipt" !== "Java"); // true
console.log("hello" !== "HELLO"); // true
console.log("script" !== "script") // false

// comparing boolean types
console.log(true !== true); // false
console.log(false !== false); // false
console.log(true !== false); // true