javascript中的几个假值

本文详细介绍了JavaScript中被视为“假”的六种特殊值:false、null、undefined、0、空字符串和NaN,并通过示例展示了这些值与其他类型进行比较时的行为。此外还探讨了在条件判断中的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前两天面试遇到的问题,当时一脸懵逼,回来想想其实之前有看过,记录下以防忘记
JavaScript中有 6 个值为“假“,这六个值是

false,null,undefined,0,”(空字符串),NaN

除了这 6 个外,其它均为“真” ,包括对象、数组、正则、函数等。注意 ‘0’、’null’、’false’、{},[]也都是真值 。

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

以上是这几个假值相比较的结果

附上面试题

var a=1,b=1,c=1,d=1;
if(a==b==c==d){
}

能不能进判断

var a =1,b=0
if(a){
}

能不能进判断

if(b){
}

能不能进判断

var m = {a:1,b:0}
if(m){
}

能不能进判断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值