Table of contents
JavaScript conditional statements allow you to execute different code blocks based on whether a particular condition is true or false. This is a fundamental concept in programming as it enables you to create dynamic programs that can adapt to different scenarios.
The most basic type of conditional statement in JavaScript is the "if statement". This statement allows you to execute a block of code only if a specified condition is true. You can also use an "if...else" statement to execute one block of code if the condition is true, and another block of code if the condition is false.
Another type of conditional statement is the "switch statement". This statement allows you to execute different blocks of code depending on the value of an expression. You can define multiple cases for different values of the expression and execute the corresponding code block if the value matches.
if statement:
The if statement is used to execute a block of code if a specified condition is true. The syntax of the if statement is as follows:
if (condition) {
// code to be executed if the condition is true
}
Here is an example:
let num = 10;
if (num > 0) {
console.log("The number is positive");
}
Output: The number is positive
if...else statement:
The if...else statement is used to execute one block of code if a specified condition is true, and another block of code if the condition is false. The syntax of the if...else statement is as follows:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Here is an example:
let num = -10;
if (num > 0) {
console.log("The number is positive");
} else {
console.log("The number is negative");
}
Output: The number is negative
switch statement:
The switch statement is used to execute different blocks of code depending on different conditions. The syntax of the switch statement is as follows:
switch (expression) {
case value1:
// code to be executed if expression is equal to value1
break;
case value2:
// code to be executed if expression is equal to value2
break;
.
.
.
default:
// code to be executed if expression doesn't match any case
break;
}
Here is an example:
let day = "Sunday";
switch (day) {
case "Monday":
console.log("Today is Monday");
break;
case "Tuesday":
console.log("Today is Tuesday");
break;
case "Wednesday":
console.log("Today is Wednesday");
break;
case "Thursday":
console.log("Today is Thursday");
break;
case "Friday":
console.log("Today is Friday");
break;
case "Saturday":
console.log("Today is Saturday");
break;
case "Sunday":
console.log("Today is Sunday");
break;
default:
console.log("Invalid day");
break;
}
Output: Today is Sunday
Summarizing Up
JavaScript conditional statements are essential for creating dynamic and interactive web applications. They allow you to control the flow of your program based on different scenarios and make your code more efficient by executing only the necessary code. Understanding conditional statements is a crucial step in learning JavaScript programming and is essential for building complex applications.