Boolean operators which return one of the operands

前端 未结 3 549
面向向阳花
面向向阳花 2020-12-21 12:24

In Python, and maybe in Javascript, the boolean or and and operators return one of the operands, instead of true or false

3条回答
  •  醉酒成梦
    2020-12-21 13:11

    As Ignacio's answer points out, these are coalescing operators. || is the null coalescing operator, && is the null-safe coalescing operator (link to follow, if I can find one sorry, I can't find a link).

    They should be available in all browsers - they are both defined in the ECMA-262 1st, 2nd, 3rd and 5th editions, most current Javascript implementations are based upon 3rd or 5th. From ECMA-262 3rd edition:

    The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:
    1. Evaluate LogicalANDExpression.
    2. Call GetValue(Result(1)).
    3. Call ToBoolean(Result(2)).
    4. If Result(3) is false, return Result(2).
    5. Evaluate BitwiseORExpression.
    6. Call GetValue(Result(5)).
    7. Return Result(6).

    The production LogicalORExpression : LogicalORExpression || LogicalANDExpression is evaluated as follows:
    1. Evaluate LogicalORExpression.
    2. Call GetValue(Result(1)).
    3. Call ToBoolean(Result(2)).
    4. If Result(3) is true, return Result(2).
    5. Evaluate LogicalANDExpression.
    6. Call GetValue(Result(5)).
    7. Return Result(6).

提交回复
热议问题