Control Statements in JavaScript
Last Updated :
20 Dec, 2023
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.");
};
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
}
}
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");
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;
}
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.");
};
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
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}.`);
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 . . .
}
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);
}
};
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.1. Decision-Making StatementsJavaScript if StatementThe if statement executes a block of cod
4 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 Statements
JavaScript statements are programming instructions that a computer executes. A computer program is essentially a list of these "instructions" designed to perform tasks. In a programming language, such instructions are called statements.Types of Statements1. Variable Declarations (var, let, const)In
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
JavaScript switch Statement
The JavaScript switch statement evaluates an expression and executes a block of code based on matching cases. It provides an alternative to long if-else chains, improving readability and maintainability, especially when handling multiple conditional branches.Switch Statement Example: Here, we will p
5 min read
JavaScript Label Statement
JavaScript label statement is used to label a block of code. A labeled statement can be used with loops and control flow statements to provide a target for the break and continue statements.Example 1: Using the break statement with labeled loops. we can terminate the outer loop from the inner loop.J
2 min read
Timing Events in Javascript
Timing events are the events that help to execute a piece of code at a specific time interval. These events are directly available in the HTML DOM (Document Object Model) Window object, i.e., they are present in the browser. So, these are called global methods and can be invoked through the 'window'
5 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