Boolean Expressions


Boolean expressions: not, and, or

For all examples assume the following declaration: var x=5, y=6;
JavaScript
Text Symbol
Description True
Expression
False
Expression
&& and (x > 0 && x < y)
(x * 2 == y + 4 && x + 1 == y)
(y >= x && y / 2 > 1 && x > 0)
(x > 0 && y < x)
(x * 2 == y + 4 && x + 1 > y)
(y >= x && y / 2 > 1 && x < 0)
¦¦ or (x > 0 ¦¦ y < x)
(x > 0 ¦¦ x < y)
(y >= x ¦¦ y / 2 < 1)
(y < x ¦¦ y / 2 > 1 ¦¦ x < 0)
(x < 0 ¦¦ y < x)
(y < x ¦¦ y < 0)
(y == x ¦¦ y / 2 < 1)
(y < x ¦¦ y / 2 < 1 ¦¦ x < 0)
! not !(x > 0 && y < x)
!(x * 2 == y + 4 && x + 1 > y)
!(x < 0 ¦¦ y < x)
!(y < x ¦¦ y < 0)
!(x > 0 && x < y)
!(x * 2 == y + 4 && x + 1 == y)
!(x > 0 ¦¦ y < x)
!(x > 0 ¦¦ x < y)

Operator Precedence

Results are shown as integers for clarity, but may include decimals.
Order Operator Description & Comments Example Result
1 () Parenthesis: Always do first, work from inside out 1 * 2 + 3 * 4 + 5
1 * (2 + 3) * 4 + 5
1 * (2 + 3) * (4 + 5)
19
25
45
2 ! Boolean not: reverses the true/false value !(x < 6)
(x >= 6)
true for 6, 7, 8, 9...
same
3 * / % Multiply, divide, remainder 2 * 3 + 4
2 * (3 % 4)
2 * (4 % 3)
10
6
2
4 + - Add, concatenate, subtract 2 + 3
"Jane" + " " + "Doe"
"two" + 3
2 + 3 * 4 - 5
5
"Jane Doe"
"two3"
9
5 < <= > >= Relational Comparisons x + 1 > 5
x / 2 <= 3
true for 5, 6, 7, 8...
true for 6, 5, 4, 3,...
6 == != Equality & inequality x != 3
x % 2 == 0
true for any x, except 3
true for any even number
7 && Boolean and: all conditions must be true (x % 2 == 0) && (x > 10)
(x < 7) && x > 0
12, 14, 16, 18...
1, 2, 3, 4, 5, 6
8 ¦¦ Boolean or: any one (or more) condition may be true (x % 2 == 0) ¦¦ (x < 0)
(x < 0 ¦¦ x > 3)
Number is even or negative
Anything not between 0 and 3
9 = Assignment y = 3 * 2 + 1 7 is saved in y

Use parenthesis!!!!


All work herein is subject to copyright. Original content to Dr. Paul Mullins, text content (Head First HTML with CSS & XHTML) to O'Reilly publishing, other tools (such as Homesite HTML-Kit and FireFox) to their respective owners.