Logical operators
There are three operators, called logical operators, that work with Boolean values. These are:
!
– logical NOT (negation)&&
– logical AND||
– logical OR
You know that when something is not true, it must be false. Here's how this is expressed using JavaScript and the logical !
operator:
> var b = !true;
> b;
false
If you use the logical NOT twice, you get the original value:
> var b = !!true;
> b;
true
If you use a logical operator on a non-Boolean value, the value is converted to Boolean behind the scenes:
> var b = "one";
> !b;
false
In the preceding case, the string value "one"
is converted to a Boolean, true
, and then negated. The result of negating true
is false
. In the next example, there's a double negation, so the result is ...
Get JavaScript : Object-Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.