The comparison
" \t\r\n" == 0
is true because the specification for the language says it should be true.
Lets start with the algorithm for Abstract Equality Comparison (x == y
)
If Type(x
) is String and Type(y
) is Number, return the result of the
comparison ToNumber(x) == y
.
See what it says there, if the first one is a string, which it is, and the second one is a number, which it is, convert the first one to a number based on the ToNumber()
operation.
So what's the ToNumber()
operation, here it is in the spec
ToNumber applied to Strings applies the following grammar to the input
String.
If the grammar cannot interpret the String as an expansion of
StringNumericLiteral, then the result of ToNumber is NaN.
The entire chapter on how different strings are converted with ToNumber
is pretty complicated, but a little further down in the spec is says
The MV of StringNumericLiteral ::: [empty]
is 0
.
The MV of StringNumericLiteral ::: StrWhiteSpace
is 0
.
(MV = Mathematical Value)
So any string that is empty, or just contain whitespace, is converted to 0
.
Lets just try and coerce the string ourselves
console.log( +" \t\r\n" ); // gives the number 0
So a string containing a whitespace, a tab, and a newline, is converted to 0
.
So, converting the string " \t\r\n"
with the internal ToNumber()
operation gives the browser 0
, so naturally 0 == 0
is true
, and that's the answer.