Week 4: Guiding Your Code with Conditional Logic
Learn how to make your JavaScript programs make decisions using if, else if, else, and switch.
Explore Chapter 4else if Statements: Checking Multiple Conditions.
The else if statement allows you to check additional conditions if the preceding if (or else if) condition was false.
Syntax
if (condition1) {
// Code for condition1 being true
} else if (condition2) {
// Code executed if condition1 is false AND condition2 is true
} else if (condition3) {
// Code executed if condition1 and condition2 are false AND condition3 is true
} // ... more else if statements can follow
- Conditions are checked in order.
- As soon as an if or else if condition evaluates to true, its block is executed, and the rest of the chain is skipped.
Example
let score = 78;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B"); // This block executes
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: D or F");
}
Since score >= 90 is false, it checks score >= 80, which is true. "Grade: B" is printed, and the remaining else if and else are skipped.
Truthy and Falsy Values.
In JavaScript, conditions don't strictly need to evaluate to true or false. Any value can be evaluated in a boolean context. Values that evaluate to true are called "truthy", and values that evaluate to false are called "falsy".
Falsy Values:
There are only a few falsy values in JavaScript:
- false
- 0 (zero)
- -0 (minus zero)
- 0n (BigInt zero)
- "" (empty string)
- null
- undefined
- NaN (Not a Number)
All other values, including non-empty strings ("hello"), non-zero numbers (42), objects ({}), and arrays ([]), are truthy.
Example:
let username = ""; // Falsy
let points = 0; // Falsy
let userList = []; // Truthy (even though empty)
if (username) {
console.log("Username exists."); // This won't run
} else {
console.log("Please enter a username."); // This will run
}
if (points) {
console.log("You have points!"); // This won't run
} else {
console.log("No points yet."); // This will run
}
if(userList){
console.log("User list exists (might be empty)."); // This WILL run
}
Understanding truthy/falsy is important for writing concise conditional checks.
Conditional (Ternary) Operator: A Concise if-else.
The ternary operator is a shorthand way to write a simple if-else statement, especially useful for assigning a value based on a condition.
Syntax
condition ? valueIfTrue : valueIfFalse
- If the condition is truthy, the expression evaluates to valueIfTrue.
- If the condition is falsy, the expression evaluates to valueIfFalse.
Example
let age = 21;
let beverage = (age >= 18) ? "Beer" : "Juice";
console.log("Allowed beverage:", beverage); // Output: Allowed beverage: Beer
let isAuthenticated = false;
let greeting = isAuthenticated ? "Welcome back!" : "Please log in.";
console.log(greeting); // Output: Please log in.
While concise, avoid nesting ternary operators excessively as it can quickly become hard to read.