Explore Control Flow
if...else Statement
Explore how to write code that runs only under certain conditions.
Explore:
- Change the variables in the examples to see how it affects what runs.
- Write your own conditional statements.
// Here is an if statementlet hungry = true;if (hungry) {console.log('Go eat!');}//Here is an if...else statementlet 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 iflet 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 + '?');}