Essential JavaScript

Strict Equality

Notice that there are two different types of equality:

  • Strict equality (=== and !==)
  • Loose Equality (== and !=)

The difference is that JavaScript will try to convert two values to be the same type for loose equality. Look at the expressions below. The result of each expression is included in a comment:

1 === true // false
1 == true // true
0 === false // false
0 == false // true

JavaScript sees both false and 0 as loosely equal. They are both represented by a 0 under the hood. Similarly, true and 1 are loosely equal and represented by a 1 under the hood.

Loose equality can cause confusion and accidents when coding. Try to stick to using === and !== instead of == and != to determine if values are equal.

Loose equality is a feature of JavaScript that allows us to take shortcuts, but more often than not it causes unexpected behavior and errors.


Learning Goals

Code Editor