Posted At Wed, Feb 21, 2024 6:51 PM

JavaScript Equality Algorithms

Listen | 2 min read

Table of Contents

  1. Introduction
  2. JavaScript Equality Algorithms
    • isLooselyEqual
    • isStrictlyEqual
    • SameValueZero
    • SameValue
  3. Conclusion

JavaScript Equality Algorithms

JavaScript offers various equality algorithms to compare values. These algorithms determine whether two values are equal and are crucial for understanding how JavaScript handles equality comparisons.

1. isLooselyEqual

The isLooselyEqual algorithm, often represented by the == operator, compares two values after converting them to a common type. It performs type coercion if the types are different.


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

2. isStrictlyEqual

The isStrictlyEqual algorithm, represented by the === operator, compares both the value and the type of the operands. It returns true only if both values are of the same type and have the same value.


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

3. SameValueZero

The SameValueZero algorithm, specified by the ECMAScript standard, determines whether two values are the same, accounting for +0 and -0 as distinct but equal values.


console.log(, -0)); // false
console.log(, NaN)); // true

4. SameValue

The SameValue algorithm, also specified by the ECMAScript standard, compares two values using the SameValue algorithm, which is similar to the strict equality (===) but handles special values differently.


console.log(, '1')); // false
console.log(, false)); // false
console.log('', false)); // false


Understanding JavaScript's equality algorithms is essential for writing reliable and bug-free code. By choosing the appropriate algorithm based on your specific use case, you can ensure the correctness of your JavaScript applications.

1260 1