Conditional statements let your code make decisions and execute different code blocks based on different conditions.
Conditional statements begin with the keyword if
. Following the keyword are parentheses where the test or condition lives, and the condition can be any expression. After the parentheses goes a code block. JavaScript evaluates the condition, and only if the resulting value is truthy will the statements inside the code block run.
Let's look at how we might use an if statement in a real application for a team running event:
Another example checking if a team member's name is entered:
Remember: JavaScript evaluates these conditions to either truthy or falsy values. Empty strings, 0, null, undefined, and NaN are all falsy values. Almost everything else is truthy.
Logical operators allow you to combine multiple conditions in sophisticated ways.
The AND operator (&&
) returns true only if both conditions are true:
In this example, the message "Welcome to the event!" will only display if both isRegistered
and hasPaid
are true.
The OR operator (||
) returns true if at least one condition is true:
Here, the message "You have access to premium features." will display if either isAdmin
or isPremiumUser
is true.
The NOT operator (!
) reverses the boolean value:
This code checks if email is NOT verified, and if so, displays a prompt to verify.
This example allows entry if the person is 18 or older AND has a ticket, OR if they are a VIP (regardless of age or ticket status).
Loops allow you to execute a block of code repeatedly until a specific condition is met.
The for loop is used when you know in advance how many times you want to execute a block of code:
A for loop has three parts:
let i = 1
- Sets a variable before the loop startsi <= 5
- Defines the condition for running the loopi++
- Increases a value each time the code block is executedThe while loop executes as long as a specified condition is true:
Warning: If you forget to increment the variable used in the condition, the loop will run forever, causing what's known as an "infinite loop."
The do-while loop is a variant of the while loop that executes the code block at least once before checking the condition:
This is useful when you want to ensure the code runs at least once, regardless of the condition.
Tip: Choose the appropriate loop based on your needs:
for
when you know the number of iterations.while
when you don't know how many times the loop will run.do-while
when you need the code to execute at least once.Switch statements offer an alternative to if-else chains when you need to compare a value against multiple options.
A switch statement evaluates an expression, matching the expression's value against a series of case clauses:
Important: Don't forget the break
statement after each case! Without it, the code will continue executing the next cases even if the condition is not met.
Practice using control flow by completing these exercises:
Remember to test your code with different inputs to make sure your control flow works correctly!