More JavaScript answers

by Danny
Published 2 min read

== 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.


_10
1 === 1; // true
_10
1 === '1'; // false, different types
_10
1 == '1'; // true, the string is converted to a number
_10
0 == ''; // true
_10
null == undefined; // true
_10
null === 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 ===.
  • null and undefined are 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 true becomes 1 and false becomes 0.
  • If one side is an object and the other a primitive, the object is converted to a primitive, usually through valueOf or toString.

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.


_10
const page = Number(searchParams.get('page'));
_10
_10
if (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.


_10
function 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.


_10
NaN === NaN; // false
_10
Object.is(NaN, NaN); // true
_10
Number.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 than true or 1 this is usually false, even for truthy values like 'yes'. Write if (x) or compare with ===.
  • Comparing form or query values without converting them. They are always strings, so input.value === 5 is never true.
  • Assuming == is faster. Modern engines optimise both, so there is no reason to pick == for speed.
  • Using switch and expecting loose matching. switch uses strict comparison, so case 1 does not match '1'.
#

Was this resource helpful?