Conditionals
Every interesting program needs to make decisions — show a welcome message if logged in, display an error if a form is incomplete, apply a discount if a user qualifies. That's what conditionals are for.
if / else
The simplest conditional runs code only if a condition is true:
const temperature = 28;
if (temperature > 25) {
console.log("It's hot outside!");
}
Add an else block to handle the false case:
const temperature = 15;
if (temperature > 25) {
console.log("It's hot outside!");
} else {
console.log("Nice and cool today.");
}
else if
Chain multiple conditions with else if:
const score = 72;
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");
}
JavaScript checks each condition from top to bottom and stops at the first one that is true.
Truthy and Falsy
In JavaScript, every value is either truthy or falsy — not just booleans:
// These are all FALSY (treated as false in conditions):
false
0
"" // empty string
null
undefined
NaN
// Everything else is TRUTHY, including:
true
1
"hello"
[] // empty array
{}
This means you can check if a value exists directly:
const username = "Alice";
if (username) {
console.log("Welcome, " + username + "!");
} else {
console.log("Please enter a username.");
}
Be careful with 0 and "" — they're falsy even though they're valid values. If you need to check whether a variable was assigned at all, check value !== undefined or value !== null instead.
The Ternary Operator
The ternary operator is a shorthand for simple if/else. It fits on one line:
// condition ? valueIfTrue : valueIfFalse
const age = 20;
const status = age >= 18 ? "adult" : "minor";
console.log(status); // "adult"
Great for assigning a value based on a condition:
const isLoggedIn = true;
const message = isLoggedIn ? "Welcome back!" : "Please log in.";
console.log(message);
switch
When comparing one value against many options, switch is cleaner than a long chain of else if:
const day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week!");
break;
case "Friday":
console.log("Almost the weekend!");
break;
case "Saturday":
case "Sunday":
console.log("It's the weekend!");
break;
default:
console.log("Midweek grind.");
}
The break statement stops the switch from falling through to the next case. The default runs if no case matches.
Try It Yourself
Enter a score...
function getGrade() { const score = Number(document.getElementById('scoreInput').value); let grade; if (score >= 90) { grade = 'A — Excellent!'; } else if (score >= 80) { grade = 'B — Great job!'; } else if (score >= 70) { grade = 'C — Good effort!'; } else if (score >= 60) { grade = 'D — Keep going!'; } else { grade = 'F — Let\'s review the material.'; } document.getElementById('result').textContent = 'Grade: ' + grade; }Have anything to say about this lesson?
Your feedback helps improve these tutorials. If something was confusing or missing, let us know.