Logical operator && and two strings in javascript

前端 未结 4 1295
北海茫月
北海茫月 2020-12-05 06:57

When I use the logical operator && between two strings, why does it give me the second string as output:

4条回答
  •  渐次进展
    2020-12-05 07:15

    Answer by @naomik pretty much covers it. If you want to go deep, I suggest taking a look at the ECMAScript specification, section 11.11:

    http://www.ecma-international.org/ecma-262/5.1/#sec-11.11

    In the "Semantics" section, we can see:

    The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:

    1. Let lref be the result of evaluating LogicalANDExpression.
    2. Let lval be GetValue(lref).
    3. If ToBoolean(lval) is false, return lval.
    4. Let rref be the result of evaluating BitwiseORExpression.
    5. Return GetValue(rref).

提交回复
热议问题