Boolean Logic:
- Boolean algebra was developed by George Boole in the mid 1800's.
- In Boolean logic a statement is either true or false. It
cannot be anything else. All variables in a Boolean algebra expression
evaluate to either true or false as well.
- In most cases we will use 0 to represent false and 1 (or any
non-zero value) to represent true. Note though that in JavaScript
that a comparison against true and a numeric value other than 1 will
evaluate to false, i.e. (10 == true) is false and (1===true) is also
false because the types on both sides of the comparison do not match.
- You can combine Boolean statements together with the fundamental Boolean
operations: AND (&&), OR (||), and NOT (!)
(and XOR (!=)) which will still result in an expression that is either
true or false.
Comparison operations:
Mathematical comparisons
These give a Boolean value (true or false) depending on the
outcome of the comparison operation.
Operation:
| True when:
|
---|
a < b | a is less than b
|
a <= b | a is less than or equal to b
|
a > b | a is greater than b
|
a >= b | a is greater than or equal to b
|
a == b | a is equal to b
|
a != b | a is not equal to b
|
a === b | a is equal to b and both are the same type (strict equality)
|
a !== b | a is not equal to b and both are the same type (strict inequality)
|
Examples:
1 < 2 | is true | | 2 < 1 | is false
|
2 == 3 | is false | | 2 != 3 | is true
|
5 === 5 | is true | | "5" === 5 | is false (types do not match)
|
We can also compare strings in JavaScript. Strings are compared
alphabetically, i.e. "a" comes before "b" and so on. Capital letters come
before lower case letters, i.e. "Z" < "a".
"abb" < "abc" | is true
|
"abc" > "Z" | is true
|
Numbers are type converted into strings for comparison against other strings,
use === or !== to avoid type conversion.
"2" <= 2 | is true | | "3" > 2 | is true
|
"2" == 2 | is true | | "2" === 2 | is false
|
Boolean Operators and Truth Tables:
A truth table is a table of all inputs (represented as 0 for false and 1 for
true) and the resulting output of the operation. We will use truth tables to
describe the Boolean operations:
NOT
| NOT a is the reverse of a, i.e. true if a is false and false if a is
true. In JavaScript ! is the Logical NOT operator.
Example: ! a
|
---|
|
|
AND
| a AND b is true if and only if both a and b are true. In JS &&
is the Logical AND operator. Example: a && b
a | b | a && b
|
---|
0 | 0 | 0
| 0 | 1 | 0
| 1 | 0 | 0
| 1 | 1 | 1
|
|
---|
OR
| a OR b is true if either a or b is true. In JS || is the Logical
OR operator. Example: a || b
a | b | a || b
|
---|
0 | 0 | 0
| 0 | 1 | 1
| 1 | 0 | 1
| 1 | 1 | 1
|
|
---|
XOR
| a XOR b is true only if a and b are not the same. This is essentially the
!= (not equal) operator.
a | b | a != b
|
---|
0 | 0 | 0
| 0 | 1 | 1
| 1 | 0 | 1
| 1 | 1 | 0
|
|
---|
Operations are combined left to right. Use () to force the order of
evaluation.
a && b || c
| →
| (a && b) || c
| !=
| a && (b || c)
|
---|
a | b | c | (a && b) || c | a && (b || c)
|
---|
0 | 0 | 0 | 0 | 0
|
0 | 0 | 1 | 1 | 0
|
0 | 1 | 0 | 0 | 0
|
0 | 1 | 1 | 1 | 0
|
1 | 0 | 0 | 0 | 0
|
1 | 0 | 1 | 1 | 1
|
1 | 1 | 0 | 1 | 1
|
1 | 1 | 1 | 1 | 1
|
More examples:
(5 < 4) || (4 < 5) | is true
|
(4 < 5) && (3 < 3) | is false
|