Explore Control Flow

if...else Statement

Explore how to write code that runs only under certain conditions.

Explore:

  1. Change the variables in the examples to see how it affects what runs.
  2. Write your own conditional statements.
// Here is an if statement
let hungry = true;
if (hungry) {
console.log('Go eat!');
}
//Here is an if...else statement
let age = 30;
if (age >= 18) {
console.log('Welcome to the voting booth!');
} else {
console.log('Sorry, you must be at least 18 to vote.');
}
// Here is an if...else statement with an else if
let favoriteBirthdayTreat = 'pie';
if (favoriteBirthdayTreat === 'cake') {
console.log('Yay! I will bake you a cake!');
} else if (favoriteBirthdayTreat === 'pie') {
console.log('I love pie too! I will bake you a pie :)');
} else {
console.log('How do you make ' + favoriteBirthdayTreat + '?');
}