Conditionals
👨💼 We need our program to respond differently based on conditions. The
if
statement lets us run code only when a condition is true.The syntax:
if (condition) {
// runs if condition is true
} else if (anotherCondition) {
// runs if first is false, this is true
} else {
// runs if all conditions are false
}
- Create a variable
scorewith a number between 0 and 100 - Write an if/else if/else chain that logs:
- "A" if score is 90 or above
- "B" if score is 80 or above
- "C" if score is 70 or above
- "F" otherwise
💰 Start with:
if (score >= 90) {
console.log('A')
}
Then add the
else if and else branches.