== converts types before comparing, while === compares type and value with no conversion. Learn the coercion rules, the surprising cases and when == null is still useful.
The short answer
#=== (strict equality) is true only when both sides have the same type and the same value. == (loose equality) first converts the values to a common type and then compares them, which produces some surprising results. Use === and !== by default.
_101 === 1; // true_101 === '1'; // false, different types_101 == '1'; // true, the string is converted to a number_100 == ''; // true_10null == undefined; // true_10null === undefined; // false
Why it happens
#Loose equality follows an algorithm in the language specification that tries to make the two sides comparable. The main rules are:
- If both values have the same type,
==behaves like===. nullandundefinedare equal to each other and to nothing else.- If one side is a number and the other a string, the string is converted to a number.
- A boolean is converted to a number first, so
truebecomes1andfalsebecomes0. - If one side is an object and the other a primitive, the object is converted to a primitive, usually through
valueOfortoString.
Those steps chain together, which is where the odd results come from:
_10'0' == false; // true: false becomes 0, '0' becomes 0_10[] == false; // true: [] becomes '' becomes 0_10[1] == 1; // true: [1] becomes '1' becomes 1_10' \t\n' == 0; // true: whitespace-only strings become 0_10'0' == ''; // false: both are strings, so no conversion
Notice that == is not even transitive: '0' == 0 and 0 == '' are both true, but '0' == '' is false.
Strict equality skips all of this. If the types differ, the answer is false.
How to fix it
#Use === and !== everywhere
Strict comparisons are easier to read, because the result never depends on hidden conversions. Turn on the eqeqeq rule in ESLint to catch accidental ==.
_10{_10 "rules": {_10 "eqeqeq": ["error", "always", { "null": "ignore" }]_10 }_10}
The null: ignore setting allows the one common exception below.
Convert explicitly when types differ
If you really do have a string and need to compare it with a number, for example a value from a form field or a URL, convert it yourself so the intent is clear.
_10const page = Number(searchParams.get('page'));_10_10if (page === 1) {_10 // ..._10}
Use == null to check for null or undefined
Because null and undefined are only loosely equal to each other, value == null is a short way to test for either. Many style guides allow this one use.
_10function greet(name) {_10 if (name == null) return 'Hello, stranger';_10 return `Hello, ${name}`;_10}
If your team prefers strict checks throughout, value === null || value === undefined does the same thing, and the ?? operator often removes the need for the check altogether.
Use Object.is for the edge cases
Even === has two quirks: NaN === NaN is false, and 0 === -0 is true. Object.is handles both.
_10NaN === NaN; // false_10Object.is(NaN, NaN); // true_10Number.isNaN(value); // the usual way to test for NaN
Common pitfalls
#- Expecting
===to compare objects by content. Two separate objects or arrays are never equal, even with identical contents, because both operators compare references for objects. - Writing
if (x == true). For any value other thantrueor1this is usually false, even for truthy values like'yes'. Writeif (x)or compare with===. - Comparing form or query values without converting them. They are always strings, so
input.value === 5is never true. - Assuming
==is faster. Modern engines optimise both, so there is no reason to pick==for speed. - Using
switchand expecting loose matching.switchuses strict comparison, socase 1does not match'1'.
Related reading
#Was this resource helpful?