Conditionals (if/else)
Make decisions in your code using if, else if, and else statements.
25 min•By Priygop Team•Last updated: Feb 2026
Making Decisions with if/else
Conditionals let your program make decisions. The if statement runs code only when a condition is true. else runs when the condition is false. else if checks additional conditions. This is how programs respond differently to different inputs.
Conditional Syntax
- if (condition) { } — Runs if condition is true
- else { } — Runs if condition is false
- else if (condition) { } — Checks another condition
- Conditions use comparison operators: >, <, ===, !==
- switch statement — Alternative for multiple conditions
- Ternary operator: condition ? valueIfTrue : valueIfFalse
if/else Example
Example
let age = 18;
// Basic if/else
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
// else if chain
let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
// Ternary operator (shorthand)
let message = age >= 18 ? "Adult" : "Minor";
console.log(message);Try It Yourself: Grade Calculator
Try It Yourself: Grade CalculatorJavaScript
JavaScript Editor
✓ ValidTab = 2 spaces
JavaScript|24 lines|573 chars|✓ Valid syntax
UTF-8