What is an if-else if Statement in JavaScript?
An if-else if statement in JavaScript allows you to evaluate multiple conditions in sequence. It enables the program to execute different blocks of code depending on which condition is true.
- If the first condition is true, its block executes, and the rest are ignored.
- If the first condition is false, the next condition is checked.
- If none of the conditions are true, the
elseblock (if provided) will run.
Syntax of if-else if Statement
How to Use an if-else if Statement
- Start with
if– The first condition is checked. - Add
else if– Additional conditions are checked if the previous ones are false. - Optional
else– A final block runs if no conditions are met.
Example:
Output:
- If
scoreis85, the output is "Grade: B". - If
scoreis95, the output is "Grade: A".
Why Use if-else if Statements?
- Multiple Condition Checks – When more than two possible outcomes exist.
- Hierarchical Decisions – To prioritize conditions in a specific order.
- Validation and Control – Apply different actions depending on the user's input or data.
- Grading Systems, Pricing, or Permissions – Ideal for systems that require different outputs for different ranges.
When to Use if-else if Statements?
- Complex Logic – When handling more than two possible scenarios.
- Range Checks – Grading systems, pricing tiers, or age verifications.
- Form Validation – Check if fields meet multiple conditions (e.g., length, format).
- Role-Based Access – Provide different access levels for different users.
Real-World Example: Age-Based Ticket Pricing
Output:
Example: Light Control Based on Time
Output:
Benefits of if-else if Statements
- Readable – Clear structure that improves code readability.
- Efficient – Once a condition is met, no further conditions are checked.
- Flexible – Allows for an unlimited number of conditions.
- Organized Logic – Helps break down complex decision-making into simpler steps.
Common Mistakes to Avoid
- Skipping
else if– Withoutelse if, multipleifstatements could lead to unnecessary checks.
Fix: Use else if to avoid running multiple blocks.
- Condition Overlap – Make sure conditions don't overlap, or the first
truecondition will execute.
Even though number is 50, the output will be "Above 40" because the second condition won’t run.
Nested if-else if Statements
You can nest if-else if statements inside each other to handle more specific cases.
Output:
Ternary Alternative (Simple if-else if)
For simpler cases, a ternary operator can replace if-else if statements.
Best Practices for if-else if
- Avoid Deep Nesting – Use
switchstatements or separate functions if the logic becomes too complex. - Order Matters – Place the most specific conditions first and general ones later.
- Simplify with Functions – Extract repeated logic into functions to improve readability.
Key Takeaways
if-else ifallows multiple condition checks in sequence.- It ensures only one block of code executes based on the first true condition.
- Ideal for handling graded, tiered, or hierarchical conditions.
- Use it when more than two outcomes are possible.