If-else statements are fundamental control flow structures in JavaScript that allow you to execute different blocks of code based on certain conditions. Mastering if-else statements is essential for writing flexible and dynamic JavaScript programs. In this chapter, we'll explore if-else statements from basic usage to more advanced techniques, covering everything you need to know to effectively use them in your code.
The basic if statement allows you to execute a block of code if a specified condition evaluates to true.
const num = 10;
if (num > 5) {
console.log("Number is greater than 5");
}
In this example, the console will log “Number is greater than 5” because the condition num > 5
evaluates to true.
The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.
const num = 3;
if (num > 5) {
console.log("Number is greater than 5");
} else {
console.log("Number is less than or equal to 5");
}
In this example, since num
is less than 5, the second block of code will be executed, and the console will log “Number is less than or equal to 5”.
The else-if statement allows you to check multiple conditions sequentially.
const num = 3;
if (num > 5) {
console.log("Number is greater than 5");
} else if (num === 5) {
console.log("Number is equal to 5");
} else {
console.log("Number is less than 5");
}
In this example, the condition num > 5
is false, so it moves to the next condition. Since num
is not equal to 5 either, the last block of code will be executed, and the console will log “Number is less than 5”.
You can also nest if-else statements within each other to handle more complex logic.
const num = 10;
if (num > 5) {
if (num < 15) {
console.log("Number is between 5 and 15");
} else {
console.log("Number is greater than or equal to 15");
}
} else {
console.log("Number is less than or equal to 5");
}
In this example, the condition num > 5
is false, so it moves to the next condition. Since num
is not equal to 5 either, the last block of code will be executed, and the console will log “Number is less than 5”.
The ternary operator is a concise way to write if-else statements in a single line.
const num = 10;
const message = num > 5 ? "Number is greater than 5" : "Number is less than or equal to 5";
console.log(message);
num
and assign it the value 10
.? :
) to assign a value to the variable message
based on a condition.num > 5
is evaluated. If it’s true, the expression before the :
is executed, and if it’s false, the expression after the :
is executed.num
is greater than 5, the value "Number is greater than 5"
is assigned to message
, otherwise "Number is less than or equal to 5"
is assigned.message
to the console.If-else statements are essential for controlling the flow of execution in JavaScript programs. By mastering if-else statements and understanding their various forms, you'll be able to write code that handles different scenarios and conditions effectively. Experiment with different if-else structures and logical combinations to deepen your understanding and proficiency. With practice and exploration, you'll become adept at using if-else statements to create robust and flexible JavaScript applications. Happy coding !❤️