Flanagan\'s O\'Reilly JavaScript book states:
Unlike the && and || operators, the ! operator converts its operand to a boolean value [...] b
Per specification, section 11.11: Binary Logical Operators:
The production [of evaluating &&
] ... is evaluated as follows:
ToBoolean(lval)
is false, return lval. The production [of evaluating ||
] ... is evaluated as follows:
ToBoolean(lval)
is true, return lval. 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 ...