您的位置:首页 > Web前端 > JavaScript

【学习笔记】JavaScript编码规范-比较运算符&相等

2015-05-18 10:51 585 查看
使用 === 和 !== 代替 == 和 !=

比较运算符进行计算时会利用ToBoolean方法进行强制转换数据类型,并遵从以下规则:

Object的计算值是true

Undefined的计算值是false

Boolean计算值是boolean的值

Numbers如果是-0,+0或者NaN,则计算值是false反之则是true

String如果是空,则计算值是false,反之则是true

if([0]){
//true
}


使用快捷方式

//bad
if(name !== ''){
}

//good
if(name){
}

//bad
if(collection.length >0){
}

//good
if(collection.length){
}

//God bless!


1:9 And God said,Let the water under the heaven be gathered together unto one place,and let the dry land appear:and it was so .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: