-
Notifications
You must be signed in to change notification settings - Fork 259
Description
This issue was first raised in a Slack post. I'm reposting here (with some additional info) so it doesn't get lost.
The original poster was using the or operator with one falsy value and one undefined value (a non-existent path expression). They noticed that the or operator sometimes returned false and sometimes returned undefined depending on the order of the operands.
I tried various combinations of the or and and operators with one or more undefined values (named foo and bar) and got the following results.
Or
true or foo // true
foo or true // true
false or foo // undefined
foo or false // false
foo or bar // undefined
And
true and foo // undefined
foo and true // undefined
false and foo // false
foo and false // undefined
foo and bar // undefined
Note that:
-
Combining a falsy value with an undefined value does indeed give different results depending on the order of the operands.
-
There's a mix of boolean and undefined results, e.g.
true or fooreturnstruebuttrue and fooreturnsundefined.
These are the only binary operations that produce such inconsistent results. Numeric operators (like + and -) return undefined if either operand is undefined. Comparison operators (like = and <) return false if either operand is undefined. And the concatenation operator (&) treats undefined operands as empty strings.
Boolean operators should have a similarly well-defined approach to undefined inputs. I suggest one of the following:
-
Treat undefined operands as
false(this would turn all of theundefinedresults above tofalse). -
Return
undefinedif either operand is undefined (all of the above expressions would then evaluate toundefined).
Any thoughts on this, @andrew-coleman?