![](/img/trans.png)
[英]Why does comparing value with undefined returns false in JavaScript?
[英]Why does `typeof false || undefined` returns “boolean”
我刚刚在控制台中执行了以下命令:
typeof false || undefined // "boolean"
虽然我希望它返回“未定义”,但是
typeof undefined // "undefined"
为什么返回“布尔值”? 如果先前运算为假值,则OR
运算符是否不应该返回最后一个参数? 如此false || undefined
false || undefined
返回undefined
并且对undefined
执行typeof
吗?
您没有遵守运算符的优先级!
> typeof false || boolean // (typeof false) || boolean
"boolean"
> typeof (false || boolean)
"undefined"
只要OR
符不是伪造的, OR
运算符就会返回left参数。
在您的示例中,我们得到了以下信息:
(typeof false) || undefined
typeof false
返回"boolean"
,这不是虚假的,因此返回而不是undefined
。
考虑代码:
typeof false || undefined
typeof false
将返回“ boolean”,因此它将变为"boolean" || undefined
"boolean" || undefined
最终输出将为“布尔值”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.