JS Conditionals
In this page:
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// code
}
The if Statement
An if statement runs its code block only when the condition inside its parentheses evaluates to true, and skips the block entirely if the condition is false.
Example: The if Statement
// Declare the constant `age`, set to `20`
// Declare the constant `age`, set to `20`
const age = 20;
// Check whether `age >= 18`
// Check whether `age >= 18`
if (age >= 18) {
// Print "You are an adult." to the console
// Print "You are an adult." to the console
console.log("You are an adult.");
}
else
An else block runs only when the preceding if condition was false, providing a fallback path so exactly one of the two blocks always runs.
An else is always tied to the nearest unmatched if that precedes it, which matters once conditionals are nested.
Example: else
// Declare the constant `age`, set to `15`
// Declare the constant `age`, set to `15`
const age = 15;
// Check whether `age >= 18`
// Check whether `age >= 18`
if (age >= 18) {
// Print "Adult" to the console
// Print "Adult" to the console
console.log("Adult");
// Otherwise, run this branch
// Otherwise, run this branch
} else {
// Print "Minor" to the console
// Print "Minor" to the console
console.log("Minor");
}
else if
else if allows checking additional conditions in sequence after the initial if, stopping and running the first block whose condition evaluates to true, useful when there are more than two possible outcomes.
Example: else if
// Declare the constant `score`, set to `75`
// Declare the constant `score`, set to `75`
const score = 75;
// Check whether `score >= 90`
// Check whether `score >= 90`
if (score >= 90) {
// Print "A" to the console
// Print "A" to the console
console.log("A");
// Otherwise, if `score >= 70`, run this branch
// Otherwise, if `score >= 70`, run this branch
} else if (score >= 70) {
// Print "B" to the console
// Print "B" to the console
console.log("B");
// Otherwise, run this branch
// Otherwise, run this branch
} else {
// Print "C" to the console
// Print "C" to the console
console.log("C");
}
Nested if Statements
An if statement can be placed inside another if statement's block, letting you check a second condition only after the first has already been confirmed true.
Example: Nested if Statements
// Declare the constant `age`, set to `25`
// Declare the constant `age`, set to `25`
const age = 25;
// Declare the constant `hasLicense`, set to `true`
// Declare the constant `hasLicense`, set to `true`
const hasLicense = true;
// Check whether `age >= 18`
// Check whether `age >= 18`
if (age >= 18) {
// Check whether `hasLicense`
// Check whether `hasLicense`
if (hasLicense) {
// Print "Can drive" to the console
// Print "Can drive" to the console
console.log("Can drive");
}
}
Ternary Operator and Short Circuit Evaluation
The ternary operator, condition ? valueIfTrue : valueIfFalse, is a compact alternative to a simple if/else, and short circuit evaluation lets && stop at the first falsy value and || stop at the first truthy value, without checking the rest.
Example: Ternary Operator and Short Circuit Evaluation
// Declare the constant `age`, set to `20`
// Declare the constant `age`, set to `20`
const age = 20;
// Declare the constant `status`, set to `age >= 18 ? "adult" : "minor"`
// Declare the constant `status`, set to `age >= 18 ? "adult" : "minor"`
const status = age >= 18 ? "adult" : "minor";
// Print `status` to the console
// Print `status` to the console
console.log(status);
// Print `true || console.log("never runs")` to the console
// Print `true || console.log("never runs")` to the console
console.log(true || console.log("never runs"));
- Forgetting curly braces around a multi-statement if block, which causes only the very next line to be conditional, not the whole intended block.
- Using a single = instead of === inside a condition, which silently assigns a value instead of comparing one.
- Writing overly deep chains of nested if statements when a cleaner else if chain, or even a switch, would be more readable.
- if runs a block only when its condition is true, and else provides a fallback when it's false.
- else if allows checking multiple conditions in sequence, stopping at the first one that's true.
- The ternary operator offers a compact form for simple two-outcome conditionals, and short circuit evaluation lets && and || skip unnecessary checks.
if, else if, else, and the ternary operator are core JavaScript syntax supported identically in every browser.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic