What values are evaluated to true and false without strict comparasion? [closed]

那年仲夏 提交于 2019-12-20 04:50:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!