Do the && and || operators convert their operands to booleans?

前端 未结 4 1033
太阳男子
太阳男子 2020-12-09 11:13

Flanagan\'s O\'Reilly JavaScript book states:

Unlike the && and || operators, the ! operator converts its operand to a boolean value [...] b

4条回答
  •  醉梦人生
    2020-12-09 11:47

    Per specification, section 11.11: Binary Logical Operators:

    The production [of evaluating &&] ... 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).

    The production [of evaluating ||] ... is evaluated as follows:

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

    So internally the value is "converted to a boolean". However, since this is never exposed -- and the entire semantic explanation is an abstraction which can be/is "optimized out" -- the behavior of && and || can be simply explained through using truthy-and-falsy values (for which ToBoolean covers: a truthy-value is one for which ToBoolean returns true, all other values are falsy).

    The logical table for && is:

    a       b      result
    truthy  any    b
    falsy   any    a

    The logic table for || is:

    a       b      result
    truthy  any    a
    falsy   any    b

    Note that either the evaluation of a or b is returned.

    Happy coding.


    For completeness, from section 9.2:

    The abstract operation ToBoolean converts its argument to a value of type boolean as ...

    • Undefined - false
    • Null - false
    • boolean (not Boolean object!) - The result equals the input argument (no conversion).
    • number (not Number object!) - The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
    • string (not String object!) - The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
    • Object - true

提交回复
热议问题