js 常见判断

JavaScript中有 6 个值为“假”,这六个值是

  1. false
  2. null
  3. undefined
  4. 0
  5. '' (空字符串)
  6. NaN

console.log( false == null )      // false

console.log( false == undefined ) // false

console.log( false == 0 )         // true

console.log( false == '' )        // true

console.log( false == NaN )       // false

 

console.log( null == undefined ) // true

console.log( null == 0 )         // false

console.log( null == '' )        // false

console.log( null == NaN )       // false

 

console.log( undefined == 0)   // false

console.log( undefined == '')  // false

console.log( undefined == NaN) // false

 

console.log( 0 == '' )  // true

console.log( 0 == NaN ) // false

结论:

1.false 除了和自身比较为 true 外,和 0,"" 比较也为 true
2.null 只和 undefined 比较时为 true, 反过来 undefined 也仅和 null 比较为 true,没有第二个
3.0 除了和 false 比较为 true,还有空字符串 ''" 和空数组 []比较也为true
4.空字符串 '' 除了和 false 比较为 true,还有一个数字 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值