||
is not an 'assign if undefined operator', although its semantics allow it to be used in a way that makes it seem like that's what it does. It's the OR operator. It evaluates the right side if and only if the left side is 'falsey'—that is, when converted to a Boolean value, it is false
. From MDN:
Returns
expr1
if it can be converted to true; otherwise, returnsexpr2
. Thus, when used with Boolean values,||
returnstrue
if either operand istrue
; if both arefalse
, returnsfalse
.
Try using a more explicit test:
output({
flag: (typeof flag === "undefined") ? true : flag;
});