Q4 of 40 · JavaScript
What is the difference between `==` and `===` in JavaScript?
Short answer
Short answer: `===` (strict equality) checks value AND type without coercion. `==` coerces types first, so `0 == false` is true but `0 === false` is false. Always use `===` in test assertions to avoid false positives from unexpected type coercion.
Detail
JavaScript offers two equality operators with very different behaviour.
Strict equality (===) compares value and type without any coercion. Two values are strictly equal only if they have the same type and the same value. This is the operator you almost always want.
Loose equality (==) performs Abstract Equality Comparison, which coerces operands to the same type first. This produces surprising results: '' == false is true, null == undefined is true, 1 == '1' is true. The coercion rules are not intuitive and differ across types.
In test automation, loose equality is dangerous because a matcher that uses == might pass when a type mismatch is actually a bug. Cypress, Jest, and Playwright assertions use strict equality internally — but your own helper functions should too.
Reference equality for objects: both operators check reference identity for objects and arrays. {} === {} is false even though the contents look the same. Use a deep-equality utility like Jest's expect(a).toEqual(b) for structural comparison.
// EXAMPLE
// Loose equality surprises
console.log(0 == false); // true (number 0 coerced)
console.log('' == false); // true (empty string coerced)
console.log(null == undefined); // true (spec exception)
// Strict equality is predictable
console.log(0 === false); // false (different types)
console.log('' === false); // false (different types)
console.log(null === undefined);// false
// In test helpers — always use ===
function assertEqual(actual, expected) {
if (actual !== expected) { // strict
throw new Error(`Expected ${expected}, got ${actual}`);
}
}