See Abstract Equality Comparison::
The comparison
x == y
, where x and y are values, produces true or false. Such a comparison is performed as follows:
So, in your situation, x
is a string, and y
is a boolean. The first condition that is fulfilled here is:
- If Type(y) is Boolean, return the result of the comparison
x == ToNumber(y)
.
Turning the check into
'\t' == 0
Which then fulfills:
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
And ToNumber('\t') === 0
:
console.log(Number('\t'));
Turning the check into
0 == 0
which is the same as
0 === 0
or true
.
Note that while a string composed of all whitespace is == false
, calling Boolean on such a string will return true
, because the string has a non-zero length:
console.log(
Boolean(' '),
Boolean('\t')
);
Of course, it would be best to always avoid ==
– use ===
instead, and you won’t have to worry about these silly coercion rules.