In JavaScript, NaN
is a "falsy" value. The ||
operator evaluates to either its first argument (if that is "truthy") or else to the second argument (if the first argument is "falsy"). Hence NaN || 9
evaluates to 9
.
From the docs for logical operators:
Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the &&
and ||
operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
and for ||
specifically:
Returns expr1
if it can be converted to true
; otherwise, returns expr2
. Thus, when used with Boolean values, ||
returns true
if either operand is true
.