Consider the following:
var a = '888';
var b = '888 ';
console.log(a); // 888
console.log(b); // 888
console.log(a == b); // false
console.log(a == 888); // true
console.log(b == 888); // true
When you compare a
and b
, they are both strings - and are compared directly, without any typecast. So whitespace at the end of b
does matter here.
However, when you compare both a
and b
to number 888, the strings stored in these variables are first converted to a number (where the trailing whitespace at the end of '888 '
is ignored) before being compared.