Open In App

Control Statements in JavaScript

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

There are several methods that can be used to perform control statements in JavaScript:

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.");
};


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
}
}

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");


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;
}

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.");
};


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

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}.`);


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 . . .
}

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);
  }
};


Output

0
2
4
6
8
10


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads