Control structures (if-else, switch) in JavaScript
Control statements are an essential part of any programming language, and JavaScript is no exception. Control statements allow us to control the flow of our code and make decisions based on certain conditions. Two common control statements in JavaScript are if-else and switch statements. In this blog, we will discuss these control statements with examples.
If-Else Statements:
An if-else statement is a conditional statement that allows us to execute different code blocks based on the value of a condition.
The basic syntax of an if-else statement is as follows:
if(condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
For example, suppose we want to check if a given number is even or odd. We can use an if-else statement to accomplish this task, as shown in the following code:
let num = 10;
if(num % 2 == 0) {
console.log(num + " is even.");
} else {
console.log(num + " is odd.");
}
In this example, if the condition "num % 2 == 0" is true, the code inside the if block will be executed, and the output will be "10 is even." If the condition is false, the code inside the else block will be executed, and the output will be "10 is odd."
Switch Statements:
A switch statement is another type of control statement that allows us to execute different code blocks based on the value of a variable or expression.
The basic syntax of a switch statement is as follows:
switch(expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
default:
// Code to execute if expression doesn't match any case
}
For example, suppose we want to print the name of a day based on its number. We can use a switch statement to accomplish this task, as shown in the following code:
let day = 5;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default: console.log("Invalid day number");
}
In this example, if the value of the "day" variable is 5, the output will be "Friday." If the value is not between 1 and 7, the output will be "Invalid day number."
Conclusion:
Control statements are essential for making decisions and controlling the flow of code in JavaScript. If-else and switch statements are two common control statements that allow us to execute different code blocks based on certain conditions. By using these statements, developers can create more complex and dynamic programs that are more responsive to user input. It's important to use these statements appropriately and efficiently to ensure the best performance and readability of the code. By mastering control statements in JavaScript, developers can create more powerful and sophisticated applications that meet the needs of their users.
Share Your Feedback Here !!