The simple version
Imagine you’re comparing two objects, like two apples. In everyday life you’d check if they’re the same apple by looking at their color, shape, and weight. In JavaScript, we use operators to compare values, and there are two common ones: == and ===.
==is the loose equality operator. It says, “If these two values look the same after you let JavaScript do its usual conversions, consider them equal.”===is the strict equality operator. It says, “They must be exactly the same type and the same value.”
Think of == as a friendly handshake that lets you change the shape of a puzzle piece to fit, while === is a lock that only opens with the exact key.
What == Does
The == operator is forgiving. Before it compares, JavaScript will try to turn both sides into a common type. This is called type coercion.
console.log(0 == false); // true
console.log('5' == 5); // true
console.log(null == undefined); // true
In the first line, false is turned into 0 so the comparison succeeds. In the second, the string '5' is converted to the number 5. In the third, null and undefined are considered equal under loose comparison.
Because == does these conversions, it can produce surprising results if you’re not careful. It’s like having a flexible measuring tape that sometimes stretches or shrinks to fit whatever you’re measuring.
What === Does
The === operator is strict. It first checks that both sides are of the same type, and only then compares the values. No conversion happens.
console.log(0 === false); // false
console.log('5' === 5); // false
console.log(null === undefined); // false
Here the comparisons fail because the types differ: a number vs. a boolean, a string vs. a number, and two distinct special values. The only time === returns true is when both type and value match exactly.
Because === is predictable, it’s the safer choice in most codebases. It’s like a lock that only opens with a key that matches every detail.
Common Pitfalls for Beginners
Expecting
==to behave like===
Many newcomers writeif (value == 0)to check for “no value” and then find that a string"0"also passes. That might be fine for some cases, but if you later compare againstfalseornull, the logic can break.Relying on type coercion unintentionally
if ('abc' == 0) { /* ... */ }This evaluates to
false, but the reason is hidden: the string is converted to a number, resulting inNaN, andNaN == 0isfalse. A beginner might not realize why the comparison fails.Mixing
==and===in the same project
When some parts of the code use loose comparison and others use strict, bugs become hard to trace. A value that passes a loose check may fail a strict check later on, leading to inconsistent behavior.Assuming
nullandundefinedare always interchangeable
They are loosely equal (==), but strictly different. If you’re checking for “no value” in a function argument, using===forces you to decide which one you truly want.
How to Choose Between Them
Default to
===.
It prevents accidental type conversions and makes your intent clear. If you’re writing a library or a team project, consistency is key.Use
==only when you need type coercion.
Common cases include checking for “falsy” values (if (!value)) or comparing withnullorundefinedin a legacy codebase where loose equality is already in use.if (value == null) { // catches both null and undefined // handle missing value }Avoid
==for numeric comparisons.
A number0is loosely equal tofalse, which can lead to bugs in logical expressions.Write tests.
If you’re unsure whether a comparison should be strict or loose, write a quick test case. Seeing the result in a test can clarify the intended behavior.
What to try next
Experiment with both operators
Open a browser console or Node REPL and run the snippets below. Notice how the results differ.console.log('' == 0); // true console.log('' === 0); // false console.log(false == 'false'); // false console.log(false === 'false'); // falseAdd a small function that uses
===function isSame(a, b) { return a === b; } console.log(isSame(10, '10')); // falseSearch for
==in a project you’re working on
Replace them with===one by one and run your test suite. Observe whether any tests fail or pass differently. This will give you a feel for the impact of strict equality.
Happy coding, and remember: when in doubt, be strict!
FAQ
What does == do in JavaScript?
== performs type coercion before comparison: 0 == false is true, "5" == 5 is true, and null == undefined is true.
When should I use ===?
Use === whenever you want a predictable comparison that requires both type and value to match exactly, such as 0 === false being false.
Why is null == undefined true?
In loose comparison, null and undefined are considered equal, but they are distinct in strict comparison where null === undefined is false.