Loops in JavaScript allow developers to repeat a block of code multiple times, based on a given condition. This is particularly useful when working with arrays, objects, or when performing repetitive tasks. There are three primary types of loops in JavaScript: for
, while
, and do...while
. In this article, we will cover each loop type with examples.
The for
loop is the most commonly used loop in JavaScript. It is ideal for iterating a specific number of times, such as when working with arrays or ranges.
A for
loop consists of three parts: initialization, condition, and iteration expression. The syntax looks like this:
for (initialization; condition; iteration) { // Code to be executed }
Example: Using a for
loop to print numbers from 1 to 5
for (let i = 1; i <= 5; i++) { console.log(i); } // Output: // 1 // 2 // 3 // 4 // 5
In this example, the loop starts with i = 1
, runs as long as i <= 5
, and increments i
by 1 after each iteration.
The while
loop repeats a block of code as long as a given condition evaluates to true
. This type of loop is useful when the number of iterations is not predetermined, and you want to continue until a certain condition is met.
The syntax of a while
loop looks like this:
while (condition) { // Code to be executed }
Example: Using a while
loop to print numbers from 1 to 5
let i = 1; while (i <= 5) { console.log(i); i++; } // Output: // 1 // 2 // 3 // 4 // 5
The while
loop continues to execute as long as the condition i <= 5
is true. In this case, i
is incremented inside the loop.
The do...while
loop is similar to the while
loop, but it guarantees that the block of code will be executed at least once, even if the condition is initially false. This is because the condition is checked after the code block has executed.
The syntax of a do...while
loop looks like this:
do { // Code to be executed } while (condition);
Example: Using a do...while
loop to print numbers from 1 to 5
let i = 1; do { console.log(i); i++; } while (i <= 5); // Output: // 1 // 2 // 3 // 4 // 5
In this example, the do...while
loop ensures that the block of code runs at least once before checking the condition i <= 5
.
Each loop type has its own advantages and is used in different scenarios:
Loops can also be nested inside other loops to perform more complex tasks. A nested loop means placing one loop inside another.
Example: Using a nested for
loop to create a multiplication table
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { console.log(i * j); } } // Output: // 1 // 2 // 3 // 2 // 4 // 6 // 3 // 6 // 9
In this example, the outer loop iterates through the numbers 1 to 3, and for each iteration, the inner loop prints the product of i
and j
.
while
, but guarantees at least one iteration.
Loops are a fundamental part of programming, allowing repetitive tasks to be automated and reducing the need for manual code repetition. Understanding the different types of loops in JavaScript—for
, while
, and do...while
—will help you write efficient and concise code when handling repeated tasks and iterations.