JavaScript - 18
JavaScript - 18: If / Else with Multiple Conditions
Use if, else if, and else when your program needs to choose between several possible results.
Your challenge: Create a grade calculator that turns a numeric score into the correct letter grade.
Check conditions from top to bottom
JavaScript checks an if / else if / else chain in order and stops when it finds the first true condition.
Start with: if (score >= 90)
Then check: score >= 80, score >= 70, and score >= 60.
The final else handles scores below 60.
Why does the order matter?
A score of 95 is also greater than 80, 70, and 60.
By checking 90 first, JavaScript gives that score an A before it reaches the lower conditions.
Try it: Change the score to 92, 84, 73, 65, and 50.
Small experiment: Test the boundary values 90, 80, 70, and 60 to make sure each one receives the expected grade.
Good JavaScript habit: When several conditions overlap, arrange them from the highest or most specific condition to the lowest or most general one.
👀 Show Solution
let score = 85;
if (score >= 90) {
console.log("Your grade is: A");
} else if (score >= 80) {
console.log("Your grade is: B");
} else if (score >= 70) {
console.log("Your grade is: C");
} else if (score >= 60) {
console.log("Your grade is: D");
} else {
console.log("Your grade is: F");
}With score = 85, the first condition is false and the second is true, so the result is B.
