LEARNING JAVASCRIPT - Trang 114

Table 5-2. Truth table for AND (

&&

)

x

y

x && y

false false false

false true

false

true

false false

true

true

true

Table 5-3. Truth table for OR (

||

)

x

y

x || y

false false false

false true

true

true

false true

true

true

true

Table 5-4. Truth table for NOT (

!

)

x

!x

false true

true

false

Looking over these tables, you’ll see that AND is

true

only if both of its operands are

true

, and OR is

false

only if both of its operands are

false

. NOT is straightforward:

it takes its only operand and inverts it.
The OR operator is sometimes called “inclusive OR” because if both operands are

true

, the result is

true

. There is also an “exclusive OR” (or XOR), which is

false

if

both operands are

true

. JavaScript doesn’t have a logical operator for XOR, but it

does have a bitwise XOR, which will be discussed later.

If you need the exclusive OR (XOR) of two variables x and y, you

can use the equivalent expression (x || y) && x !== y.

90 | Chapter 5: Expressions and Operators