Does Javascript eval correctly evaluate tri-state boolean logic?
Define "correct". Javascript defines the behavior. Eval
in Javascript evaluates the string as a javascript code. SQL defines the behavior as well. The behavior is different in both.
In javascript, null
acts like false
in boolean expressions (is falsy).
0
, NaN
, ""
, null
, undefined
(and of course false
) are all falsy. Objects, non-empty strings and non-zero numbers (and of course true
) are all truthy.
&&
returns the first falsy argument (if any) or the last argument (lazy AND) and does not evaluate the rest. null && "anything"
is null
. This can be used in statements like console && console.log && console.log()
.
||
returns the first truthy argument (if any) or the last argument (lazy OR) and does not evaluate the rest. null || "something"
is "something"
. This can be used in statements like var xhr = XmlHttpRequest || ItsReplacementInOlderBrowsers
!null
evaluates to true
. if(null) ...
evaluates the else
branch. The same applies to anything falsy.
Technically, undefined variables are undefined
, not null
. Both are falsy, though.