!= and == vs. !== and ===
来源:互联网 发布:淘宝装修免费模块 编辑:程序博客网 时间:2024/06/16 18:16
!= and == vs. !== and ===
A common mistake that developers are susceptible to is the lack of understanding of false
values in JavaScript. In JavaScript, null, 0, ‘’, false, and undefined are all equal (==) to each
other, since they all evaluate to false. This means that if you use the code test == false, it
will evaluate true if test is also undefined or equal to null, which may not be what you want.
This is where !== and === become useful. Both of these operators look at the explicit
value of the variable (such as null), not just what it is equivalent to (such as false). JSLint
requires that anytime you use != or == against a falselike value, you must use !== or ===
instead. Listing 3-12 shows some examples of how these operators differ.
上文摘自John Resig的《Pro JavaScript Techniques》,但是有些错误。
如果用==对null, undified, '', false, 0进行判断,结果并不像书上所说null == false,我对这5个值分别判断结果如下(IE7和Firefox结果一致):
==nullundified''false0null√ undified√√ ''××√ false××√√
0××√√√
可以看到, null == undifed, '' == false, '' == 0, false == 0
但是null, undified, '', 0都可以在bool转化时转成false, 比如 if(null), if(undified), if(''), if('')和if(false)等效.
这点可用
document.writeln(!null);
document.writeln(!undefined);
document.writeln(!'');
document.writeln(!false);
document.writeln(!0);
检验.
- != and == vs. !== and ===
- "==" and "equals"
- java(== and equals)
- copy constructor and = operator
- Copy Constructor and operator=
- shell test -eq and =
- Function parameter types and =>
- ==, .equals(), compareTo(), and compare() (之一)
- Host='localhost' and User='root';
- ==, .equals(), compareTo(), and compare() (之一)
- SQLSTATE=55025 and SQLSTATE=55025
- Mysql字符串模糊查询<= and <=
- result of += in C and C++
- inverse = “true” example and explanation
- inverse = “true” example and explanation
- myeclipse 瘦身 and exit code= 13
- codeforces 381A. Alyona and copybooks=
- input type="file" and FormData
- 引入敏捷但不行之过甚
- 新手上路 -- Mac OS X(桌面1)
- uclinux内核的console(2):early console
- ACE备忘录
- rsyncd - 认证相关参数配置说明
- != and == vs. !== and ===
- c#多线程Semaphore类旗语锁定
- 软件项目管理经验总结
- div+css 面试题目的简介
- 转--新手必须了解的ASP.NET语句
- uclinux内核的console(3):console驱动初始化
- 完美企业网站的101项指标
- rsync - 客户端其他参数
- 张锦贵