Control Statements in JavaScript
Last Updated :
13 Jun, 2025
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:
JavaScript
const num = 5;
if (num > 0) {
console.log("The number is positive.");
};
OutputThe number is positive.
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:
JavaScript
let num = -10;
if (num > 0)
console.log("The number is positive.");
else
console.log("The number is negative");
OutputThe number is negative
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:
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.");
};
OutputNumber 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
JavaScript
let num = 10;
let result = num >= 0 ? "Positive" : "Negative";
console.log(`The number is ${result}.`);
OutputThe 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:
JavaScript
for (let i = 0; i <= 10; i++) {
if (i % 2 === 0) {
console.log(i);
}
};
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
JavaScript
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:
JavaScript
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.
Similar Reads
JavaScript- Control Flow Statements Control flow statements in JavaScript control the order in which code is executed. These statements allow you to make decisions, repeat tasks, and jump between parts of a program based on specific conditions.JavaScript if StatementThe if statement executes a block of code only if a specified conditi
3 min read
JavaScript - Conditional Statements JavaScript conditional statements allow you to execute specific blocks of code based on conditions. If the condition is met, a particular block of code will run; otherwise, another block of code will execute based on the condition.1. Using if StatementThe if statement is used to evaluate a particula
4 min read
JavaScript Common Mistakes JavaScript is an easy language to get started with, but achieving mastery takes a lot of effort, time, and patience. Beginners often make a few well-known mistakes. In this article, weâll cover some of the most common learning mistakes people make and find out how to overcome them. Many of these tip
4 min read
Getting Started with JavaScript JavaScript is a lightweight, single-threaded, dynamically typed, interpreted programming language with object-oriented capabilities. Primarily JavaScript is used to add interactivity and dynamism to web pages. It breathes life into static HTML and CSS, enabling features like dynamic content updates,
8 min read
Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at
7 min read
How to Set Time Delay in JavaScript? Delaying the execution of code is a fundamental technique that is commonly used in JavaScript for tasks like animations, API polling, or managing time intervals between actions. JavaScript provides several built-in methods to set time delays: setTimeout() and setInterval(). We can set time delay in
2 min read
How to take user input in JavaScript? Interacting with users is the best way to create engaging and dynamic web applications. Taking user input allows your applications to be interactive and responsive. Here we will see the approach to take user input in JavaScript, specifically using the prompt method, which is perfect for beginners.Ap
3 min read
How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
4 min read
How to trigger events in JavaScript ? JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences
2 min read
JavaScript Course Variables in JavaScript Variables in JavaScript are containers that hold reusable data. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory loca
4 min read