Loose vs strict equality (== vs ===) in JavaScript
Intermediate · 4 min read
JavaScript has two equality operators. === is strict: two values are equal only if they are the same type and the same value. == is loose: if the types differ, it converts one or both values first, then compares. Those conversions follow rules that almost nobody remembers, and they produce results like [] == false being true.
In this tutorial you will compare five pairs of values with both operators: a number and its string, an empty string and zero, null and undefined, null and zero, and an empty array and false. You will work out why each loose answer comes out the way it does, and finish with NaN and typeof null, two oddities worth knowing. You need to know the basic types: numbers, strings, booleans, null and undefined.
Strict equality refuses to convert
=== checks the type first. A number and a string are different types, so 0 === '0' is false straight away, no conversion. That predictability is why most style guides say to use === everywhere.
Loose equality converts, mostly to numbers
When == sees two different types, it usually turns them into numbers and compares those. '0' becomes 0, '' becomes 0, false becomes 0, true becomes 1. An object, including an array, is first turned into a string. null and undefined have their own special rule, which you will see in Step 3.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
100 Loose equality, and the comparisons that surprise
fairCompare a few pairs of values with == and with ===: a number against its string, an empty string against zero, null against undefined and against zero, and an empty array against false. Also exercise 34, which asks what type a value is; here the angle is what happens when two types are compared.
Stuck? Three hints, smallest first
- 1.
==converts before it compares;===refuses and answers false. - 2.
nullis loosely equal toundefinedand to nothing else at all. - 3. An array converts to a string first, and an empty array becomes an empty string.
Step 1: a number and its string
console.log(0 == '0'); // true
console.log(0 === '0'); // false
== turned '0' into the number 0, then compared 0 with 0. === saw a number and a string and said no.
This is the conversion people hope for when they use == with form input, which is always text. Converting on purpose is clearer: Number(input) === 0.
Step 2: an empty string and zero
console.log('' == 0); // true
console.log(Number('')); // 0
console.log('' === 0); // false
An empty string turns into 0 as a number. So a blank form field is loosely equal to zero, which is rarely what you want: "no answer" and "the answer is zero" are different things.
Step 3: null and undefined
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log(null == 0); // false
console.log(null == false); // false
console.log(null == ''); // false
Here the "convert to numbers" idea breaks down. null is not turned into 0 for ==. The rule for null is special: it is loosely equal to undefined and to nothing else at all.
That makes value == null a handy one-line check for "is it null or undefined?", the same question the ?? operator asks. It is the one use of == that many careful developers allow.
Step 4: an empty array and false
This is the famous one:
console.log([] == false); // true
console.log(String([])); // (an empty string)
console.log(Number([])); // 0
== works it out in steps. false becomes the number 0. The array is an object, so it is turned into a string first, and an empty array becomes ''. Then '' becomes 0. Finally 0 == 0 is true. Three hidden conversions, and an empty list is "equal to false". Yet in an if, [] counts as true, because every object does. The two rules disagree.
Step 5: all five at once
const pairs = [
[0, '0'],
['', 0],
[null, undefined],
[null, 0],
[[], false],
];
console.log(pairs.map(([a, b]) => a == b)); // [ true, true, true, false, true ]
console.log(pairs.map(([a, b]) => a === b)); // [ false, false, false, false, false ]
Strict equality says false to every pair, because no pair has two values of the same type. Loose equality says true to four of the five.
Step 6: two oddities
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
console.log(typeof null); // object
NaN ("not a number") is the result of failed math like Number('abc'). It is not equal to anything, not even itself, so x === NaN is always false. Use Number.isNaN(x) instead.
typeof null says 'object', even though null is not an object. It's a mistake from the first version of JavaScript that can't be fixed now without breaking old websites. To check for null, write value === null.
The full solution
The answer panel runs all five pairs with both operators, shows what an empty array becomes as a string and as a number, and checks null, NaN and typeof null. Press Run to see the output.
const pairs = [
[0, '0'],
['', 0],
[null, undefined],
[null, 0],
[[], false],
];
const loose = pairs.map(([a, b]) => a == b);
const strict = pairs.map(([a, b]) => a === b);
console.log('loose: ', loose);
console.log('strict:', strict);
console.log('an empty array as a string:', JSON.stringify(String([])), 'as a number:', Number([]));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('loose equality converts the string to a number', 0 == '0', true);
test('strict equality just says no', 0 === '0', false);
test('an empty string is loosely zero', '' == 0, true);
test('null is loosely equal to undefined', null == undefined, true);
test('but not to zero, and not to anything else', [null == 0, null == false, null == ''], [false, false, false]);
test('an empty array is loosely false', [] == false, true);
test('because it becomes an empty string first, then zero', [String([]), Number([])], ['', 0]);
test('typeof null is object, a bug too old to fix', typeof null, 'object');
test('and NaN is not equal to itself, so ask Number.isNaN', [NaN === NaN, Number.isNaN(NaN)], [false, true]);
Also exercise 34, there asking what type a value is; here the angle is what == does when the two types differ. It converts, following a table nobody remembers, and the results include '' == 0 and [] == false both being true — the array becomes '', which becomes 0, which equals false's 0. Use === and convert deliberately, and these stop being trivia.
The two exceptions worth keeping in your head. null == undefined is true, and both are false against everything else, which makes value == null a genuinely useful one-line "is it missing?" — the same question ?? asks. And NaN === NaN is false, so indexOf(NaN) never finds it and x === NaN is always false; Number.isNaN is the only reliable test, which is why the assertions in this course check Number.isNaN(x) against true rather than comparing to NaN. typeof null being 'object' is the oldest wart in the language and can never be fixed without breaking pages that depend on it.
Common mistakes
- Using
==with form values.'' == 0istrue. Convert withNumber()and compare with===. - Checking for
NaNwith===. It's never equal to itself. UseNumber.isNaN. - Using
typeof x === 'object'to rule outnull.typeof nullis'object'too. Addx !== null.
Try it yourself
- Predict, then check:
'1' == 1,'1' == true,'2' == true. - Write
isMissing(x)withx == null, and test it withnull,undefined,0and''. - Find out what
[1] == 1and[1, 2] == '1,2'give, and explain each one using Step 4.