Control Statements in JavaScript
JavaScript control statement is used to control the execution of a program based on a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.
Types of Control Statements in JavaScript
- Conditional Statement: These statements are used for decision-making, a decision
- n is made by the conditional statement based on an expression that is passed. Either YES or NO.
- Iterative Statement: This is a statement that iterates repeatedly until a condition is met. Simply said, if we have an expression, the statement will keep repeating itself until and unless it is satisfied.
Approach 1: If Statement
In this approach, we are using an if statement to check a specific condition, the code block gets executed when the given condition is satisfied.
Syntax
if ( condition_is_given_here ) {
// If the condition is met,
//the code will get executed.
}
Now let's understand this with the help of example:
const num = 5;
if (num > 0) {
console.log("The number is positive.");
};
Output
The number is positive.
Approach 2: Using If-Else Statement
The if-else statement will perform some action for a specific condition. If the condition meets then a particular code of action will be executed otherwise it will execute another code of action that satisfies that particular condition.
Syntax
if (condition1) {
// Executes when condition1 is true
if (condition2) {
// Executes when condition2 is true
}
}
Now let's understand this with the help of 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
Approach 3: Using Switch Statement
The switch case statement in JavaScript is also used for decision-making purposes. In some cases, using the switch case statement is seen to be more convenient than if-else statements.
Syntax
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
Now let's understand this with the help of example:
let num = 5;
switch (num) {
case 0:
console.log("Number is zero.");
break;
case 1:
console.log("Nuber is one.");
break;
case 2:
console.log("Number is two.");
break;
default:
console.log("Number is greater than 2.");
};
Output
Number is greater than 2.
Approach 4: Using the Ternary Operator (Conditional Operator)
The conditional operator, also referred to as the ternary operator (?:), is a shortcut for expressing conditional statements in JavaScript.
Syntax
condition ? value if true : value if false
Now let's understand this with the help of example
let num = 10;
let result = num >= 0 ? "Positive" : "Negative";
console.log(`The number is ${result}.`);
Output
The number is Positive.
Approach 5: Using For loop
In this approach, we are using for loop in which the execution of a set of instructions repeatedly until some condition evaluates and becomes false
Syntax
for (statement 1; statement 2; statement 3) {
// Code here . . .
}
Now let's understand this with the help of example:
for (let i = 0; i <= 10; i++) {
if (i % 2 === 0) {
console.log(i);
}
};
Output
0 2 4 6 8 10
Approach 6: Using While loop
The while loop repeats a block of code as long as a specified condition is true.
Syntax
while (condition) {
// code block
}
Now let's understand this with the help if example
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Output
1
2
3
4
5
Approach 7: Using Do-While loop
The do-while loop is similar to the while loop, except that the condition is evaluated after the execution of the loop's body. This means the code block will execute at least once, even if the condition is false.
Syntax
do {
// code block
} while (condition);
Now let's understand this with the help of example:
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
Output
1
2
3
4
5
Conclusion
This article now includes examples for if statement, if-else statement, switch statement, ternary operator, for loop, while loop, and do-while loop, providing a comprehensive guide to control statements in JavaScript. These control structures help manage the flow of a program based on various conditions and are fundamental to mastering JavaScript.