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 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.
There are several methods that can be used to perform control statements in JavaScript.
- If Statement
- Using If-Else Statement
- Using Switch Statement
- Using the ternary operator (conditional operator)
- Using For loop
We will explore all the above methods along with their basic implementation with the help of examples.
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.
}
Example: In this example, we are using an if statement to check our given condition.
Javascript
const num = 5; if (num > 0) { console.log( "The number is positive." ); }; |
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
}
}
Example: In this example, we are using the if..else statement to verify whether the given number is positive or negative.
Javascript
let num = -10; if (num > 0) console.log( "The number is positive." ); else console.log( "The number is negative" ); |
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;
}
Example: In this example, we are using the above-explained approach.
Javascript
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." ); }; |
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
Example: In this example, we are using the ternary operator to find whether the given number is positive or negative.
Javascript
let num = 10; let result = num >= 0 ? "Positive" : "Negative" ; console.log(`The number is ${result}.`); |
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 . . .
}
Example: In this example, we are using Iterative Statement as a for loop, in which we find the even number between 0 to 10.
Javascript
for (let i = 0; i <= 10; i++) { if (i % 2 === 0) { console.log(i); } }; |
0 2 4 6 8 10