问题
I want to have a list of all values that will be evaluated to true
or false
with poor equality check like !=
, ==
or even if()
I had one list that I collected throught years, but it died alog with my PC
回答1:
http://bonsaiden.github.com/JavaScript-Garden/#types.equality
"It's better to learn the coercion rules rather than try to memorize the results of god knows how many different comparisons"...
...But here's a list ;)
==
"" == "0" // false
0 == "" // true
0 == "0" // true
false == "false" // false
false == "0" // true
false == undefined // false
false == null // false
null == undefined // true
" \t\r\n" == 0 // true
===
"" === "0" // false
0 === "" // false
0 === "0" // false
false === "false" // false
false === "0" // false
false === undefined // false
false === null // false
null === undefined // false
" \t\r\n" === 0 // false
Objects
{} === {}; // false
new String('foo') === 'foo'; // false
new Number(10) === 10; // false
var foo = {};
foo === foo; // true
NaN == NaN // false
NaN === NaN // false
NaN == false // false
//NaN does not coerce using non-strict equality.
Update 29/01/14:
Added NaN
for completeness.
来源:https://stackoverflow.com/questions/9417201/what-values-are-evaluated-to-true-and-false-without-strict-comparasion