Search⌘ K
AI Features

Use === Instead of ==

Understand the differences between == and === operators in JavaScript, and learn why === provides strict equality checks without type coercion. This lesson helps you avoid common pitfalls by using === for accurate comparisons, improving code reliability and preventing unexpected results.

Many JavaScript programmers often repeat the mistake of comparing using ==, which is the type-coercion non-strict equality operator.

Example of == operator

Let’s look at an example that shows why using == may be a bad idea.

Javascript (babel-node)
const a = '1';
const b = 1;
const c = '1.0';
console.log(a == b);
console.log(b == c);
console.log(a == c);

Explanation

In the short piece of code given above, the constants a, b, and c have the values ‘1’, 1, and ‘1.0’, respectively. One value is of number type, and the other two are of string type. The ...