[英]Javascript : Strange behaviour `empty string` AND `false` returns empty string
Today I came across the strange behaviour in Javascript. 今天,我遇到了Javascript中的奇怪行为。 Below is the code
下面是代码
return "" && false
returns "". 返回“”。
Why it behaves so ? 为什么会这样呢?
Because 因为
The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:
产生LogicalANDExpression:LogicalANDExpression && BitwiseORExpression的评估如下:
- Let lref be the result of evaluating LogicalANDExpression.
令lref为评估LogicalANDExpression的结果。
- Let lval be GetValue(lref).
令lval为GetValue(lref)。
- If ToBoolean(lval) is false, return lval.
如果ToBoolean(lval)为false,则返回lval。
- Let rref be the result of evaluating BitwiseORExpression.
令rref为评估BitwiseORExpression的结果。
- Return GetValue(rref).
返回GetValue(rref)。
This means: 这意味着:
Return the first value if it is falsy, return the second value if the first is truthy.
如果第一个值是假,则返回第一个值;如果第一个值是真,则返回第二个值。
This is also the behavior seen if you do: 这也是您执行以下操作时看到的行为:
return false && true
You get false
. 你
false
。
This means also that this 这也意味着
return 23 && "Hello"
would give you "Hello" 会给你“你好”
LHS首先运行,返回使您退出该函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.