JS: Equality

By Xah Lee. Date: . Last updated: .

Double Equal

The double equal == does type conversion implicitly.

console.log([] == "");
console.log("0" == 0);
console.log("" == 0);
console.log(1 == true);
// all true

Tip: never use double equal. Use triple equal instead.

Triple Equal

Triple Equal can compare most Primitive Values such as string, number, Symbol, undefined, null, except NaN.

Object/Array cannot be compared with triple equal.

console.log("x" === "x"); // true
console.log(3 === 3); // true
console.log(3 === 3.0); // true
console.log(Infinity === Infinity); // true
console.log(NaN === NaN); // false
console.log([] === []); // false
console.log({} === {}); // false

Triple equale does not convert value types implicitly.

console.log(([] === "") === false);
console.log(("" === {}) === false);
console.log(("0" === 0) === false);
console.log(("" === 0) === false);
console.log((1 === true) === false);
// all true

Test NaN

Test Infinity

Number.isFinite

Test Equality of Objects

Test Equality of Objects

βˆ‘ JS in Depth
XAH
Buy Xah JavaScript Tutorial
βˆ‘ JS in Depth
XAH

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Regular Expression

Date

Set Object

Map Object

Number

Misc