Javascript:false || undefined vs undefined ||假 [英] Javascript: false || undefined vs undefined || false
问题描述
||行为的解释是什么运算符(逻辑OR),在JavaScript中与 false
和未定义
一起使用时?
What is the explanation for behavior of the "||" operator (logical OR), when using it with false
and undefined
on both sides in JavaScript?
1)
> false || undefined
undefined
2)
> undefined || false
false
推荐答案
逻辑 OR
运算符不可交换,如 +
, *
等。返回第一个可以转换为 true
的表达式。 (来源 Mozilla Doc )
The logical OR
operator isn't commutative like +
, *
, etc. It returns the first expression which can be converted into true
. (Source Mozilla Doc)
-
在
false ||根据定义,未定义的
,false
无法转换为true
(因为它是相反的),所以它返回第二个操作数(undefined
)
In
false || undefined
,false
can't be converted totrue
by definition (since it's the opposite), so it returns the second operand (undefined
)
在中未定义|| false
, undefined
是一个值,但在Javascript中被视为 false
,所以逻辑运算符计算第二个操作数并返回 false
(因为两个操作数均为假)。
In undefined || false
, undefined
is a value, but considered as false
in Javascript, so the logical operator evaluate the second operand and returns false
(because both operands are false).
这篇关于Javascript:false || undefined vs undefined ||假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!