Back to course

Strict Equality (=== and !==): Avoiding Type Coercion

JavaScript: The Complete '0 to Hero' Beginner Course

29. Strict Equality (=== and !==)

Strict Equality is the preferred way to compare values in JavaScript. It checks two conditions:

  1. Are the values the same?
  2. Are the data types the same?

If both are true, it returns true; otherwise, it returns false. It performs no type coercion.

Strict Equality (===)

javascript let number = 10; let string = '10';

console.log(number === 10); // true (Same type, same value) console.log(number === string); // false (Different types: number vs string)

console.log(0 === false); // false (Different types: number vs boolean) console.log(null === undefined); // false (Different types)

Strict Inequality (!==)

This checks if the values are not strictly equal (i.e., if the type or the value differs).

javascript console.log(10 !== '10'); // true (Types are different) console.log(5 !== 5); // false

Golden Rule: Always use === and !== unless you have a very specific, advanced reason to use loose comparison.