为什么新布尔值(false)为真? [英] Why new Boolean(false) is true?

查看:183
本文介绍了为什么新布尔值(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)为真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆