javascript interactive

JavaScript Interactive — a console crash course


1.5 Boolean Logic

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
true && false
false
The && operator is the logical AND of two boolean values.
>>>
true || aMissingVariable
true
The || operator is the logical OR. The second argument is not evaluated if the first is true.
>>>
!true
false
The ! operator is the logical NOT (negation).
>>>
false || "value"
"value"
Although the && and || operators coerce their operands to boolean values, they return the value of the last operand evaluated.
>>>
null || ""
""
The || operator thereby becomes a convenient way of setting a default value. But watch out for 0...
>>>
!!0
false
Using double negation, we force boolean type coercion. The undefined, null, 0, NaN and "" values are all false. Other values are true.
>>>
!!42
true
>>>
!!""
false
>>>
!!" "
false
>>>
!!({})
true
>>>

Index 1 2 3 4 5 6 7