[英]Why does `typeof false || undefined` returns “boolean”
I've just executed the following in console: 我刚刚在控制台中执行了以下命令:
typeof false || undefined // "boolean"
While I expected it to return "undefined", since 虽然我希望它返回“未定义”,但是
typeof undefined // "undefined"
Why did it return "boolean"? 为什么返回“布尔值”? Isn't the
OR
operator supposed to return the last argument if previous evaluated to falsey values? 如果先前运算为假值,则
OR
运算符是否不应该返回最后一个参数? So that false || undefined
如此
false || undefined
false || undefined
returns undefined
and typeof
is executed against undefined
? false || undefined
返回undefined
并且对undefined
执行typeof
吗?
You're not observing operator precedence! 您没有遵守运算符的优先级!
> typeof false || boolean // (typeof false) || boolean
"boolean"
> typeof (false || boolean)
"undefined"
The OR
operator returns the left argument as long as it isn't falsy. 只要
OR
符不是伪造的, OR
运算符就会返回left参数。
In your example, we've got this: 在您的示例中,我们得到了以下信息:
(typeof false) || undefined
typeof false
returns "boolean"
, which isn't falsy, so it is returned rather than undefined
. typeof false
返回"boolean"
,这不是虚假的,因此返回而不是undefined
。
Consider the code : 考虑代码:
typeof false || undefined
typeof false
will return "boolean" , so it will become "boolean" || undefined
typeof false
将返回“ boolean”,因此它将变为"boolean" || undefined
"boolean" || undefined
The final output will be "boolean" 最终输出将为“布尔值”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.