本文介绍了为什么新布尔值(false)为真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白发生了什么
var x=new Boolean(false) if(x){ console.log('plus') } console.log(x==false) //true为什么 if(x)返回 true ?
推荐答案在文档中说:
任何值未定义或为null的对象(包括值为false的Boolean 对象)在传递给条件语句时计算结果为true。例如,以下条件如果语句的计算结果为真
Any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true
x = new Boolean(false); if (x) { // ...this code is executed }
直接来自 MDN 。
更多推荐
为什么新布尔值(false)为真?
发布评论