if Statement With Multiple Boolean Expressions
Sometimes we want to have multiple conditions because one is not enough.
This OR
That, This AND
That¶
- "If I were taller
OR
that shelf was lower, I could reach that can of soup." - "Could I have 20 ones
OR
a ten and 10 onesOR
even 2 fives and 10 ones?" - "You have to wear a shirt
AND
shoes to enter this premise!" - "If you are older than 5
AND
younger than 12 then the cost is $1"
The Logical OR ||
¶
In JavaScript the logical OR is ||
. That is two vertical bars. That key is in different places depending on the keyboard. Where is it on yours? You use it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
This is wrong¶
1 2 3 4 |
|
This is correct¶
1 2 3 4 |
|
Either one of the expressions can be true and the whole thing is true!¶
The Logical AND &&
¶
In JavaScript the logical AND is &&
, two ampersands. Here's how we use it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Both of the boolean expression have to be true. If either one is false then the whole thing is false.¶
Here's a logical AND example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
** Click me to run multipleBooleans3()
**
Take note of the
return
statement in this program. This will stop the program from finishing the function. We will learn more about this in upcoming units. For now, just know that it will stop this function as soon as thereturn
statement is reached.
Here's an if/else
¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
** Click me to run multipleBooleans4()
**
Note: Refresh the page to return to the course content
Demo
- Demo: Drivers License
- unit03/demos/demo-drivers-license.html
Read more¶
Related Reading
Pages 46 - 48
Hands on Work¶
Labs
- Lab06: Multiple Booleans
- unit03/labs/lab-06-multipleBooleans.html
Exercises
- Exercise for lab05
- unit03/exercises/exercise-05.html