Skip to content

Control Flow Statements

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.

Conditional Statements

Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run.

IF

If the condition true,then the statements inside the if block will execute.

if (condition) {
// statements to execute
}
// example
let age = 18;
if (age >= 18) {
console.log('You can sign up');
}

IF ELSE

You can use an if statement to check whether a condition is true and execute a block of code accordingly. If the condition is false, you can use an else statement to execute a different block of code.

if( condition ) {
// ...
} else {
// ...
}

IF ELSE IF

The if , if…else statements accepts a single condition and executes the if or else block accordingly based on the condition.

To check multiple conditions and execute the corresponding block if a condition is true, you use the if...else...if statement like this:

if (condition1) {
// ...
} else if (condition2) {
// ...
} else if (condition3) {
//...
} else {
//...
}

BREAK

The break statement is used to exit a loop (or a switch statement) prematurely, regardless of the condition.

// syntax
break;
//example
for (let i = 0; i < 5; i++) {
if (i === 3) {
break; // Exit the loop when i equals 3
}
console.log(i);
}
// output
// 0
// 1
// 2

CONTINUE

The continue statement is used to skip the current iteration of the loop and move to the next iteration, without executing the remaining code in the current iteration.

// syntax
continue;
//example
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue; // Skip the current iteration when i equals 3
}
console.log(i);
}
// output
// 0
// 1
// 2
// 4

SWITCH CASE

The switch statement evaluates an expression, compares its results with case values, and executes the statement associated with the matching case value.

switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
case value3:
statement3;
break;
default:
statement;
}

Iterative Statements

In JavaScript, loops are used to repeatedly execute a block of code based on a condition. They are particularly useful when you need to repeat a task multiple times without writing the same code over and over.

JavaScript provides several types of loops, including for, while, do...while, and for...in (for iterating over properties of an object) or for...of (for iterating over elements of an array or iterable object).

WHILE

A while loop repeats as long as the condition is true. The condition is checked before each iteration.

while (condition) {
// Code to be executed
}

DO WHILE

A do...while loop is similar to a while loop, but it checks the condition after executing the loop. This guarantees that the code inside the loop will execute at least once, even if the condition is initially false.

do {
// Code to be executed
} while (condition);

FOR

A for loop repeats a block of code a specified number of times. It consists of three parts: initialization, condition, and increment/decrement.

for (initialization; condition; increment/decrement) {
// Code to be executed
}
// example
for (let i = 0; i < 5; i++) {
console.log(i);
}
//expected output
// 0
// 1
// 2
// 3
// 4

FOR IN

The for...in loop is used to iterate over the properties of an object.

let person = { name: "John", age: 30, city: "New York" };
for (let key in person) {
console.log(key + ": " + person[key]);
}
// output
// name: John
// age: 30
// city: New York

FOR OF

The for...of loop is used to iterate over iterables (like arrays, strings, etc.) and returns the values directly.

let arr = [10, 20, 30, 40];
for (let number of arr) {
console.log(number);
}
// output
// 10
// 20
// 30
// 40