Loops in JavaScript

Javascript 9 min min read Updated: Mar 09, 2026 Beginner
Loops in JavaScript
Beginner Topic 8 of 15

Loops in JavaScript

In programming, there are many situations where a block of code needs to run multiple times. Instead of writing the same code repeatedly, developers use loops. Loops allow a program to execute a block of code again and again until a specific condition is met.

For example, if you want to display numbers from 1 to 10 or process items in a list, loops make the task much easier and more efficient.

What are Loops?

A loop is a programming structure that repeats a block of code as long as a specified condition is true. Loops are widely used in applications for processing data, generating outputs, and automating repetitive tasks.

JavaScript provides several types of loops:

  • for loop
  • while loop
  • do...while loop
  • for...of loop
  • for...in loop

1. The for Loop

The for loop is the most commonly used loop in JavaScript. It is typically used when the number of iterations is known in advance.

javascript for (let i = 1; i <= 5; i++) { console.log(i); }

Output:

output 1 2 3 4 5

The for loop consists of three parts:

  • Initialization – Starting value of the loop variable
  • Condition – Determines how long the loop runs
  • Increment/Decrement – Updates the loop variable after each iteration

2. The while Loop

The while loop runs as long as a specified condition remains true. It is useful when the number of iterations is not known beforehand.

javascript let i = 1; while (i <= 5) { console.log(i); i++; }

This loop continues until the condition becomes false.

3. The do...while Loop

The do...while loop is similar to the while loop, but it always executes the code block at least once before checking the condition.

javascript let i = 1; do { console.log(i); i++; } while (i <= 5);

Even if the condition is false, the code inside the loop runs once.

4. The for...of Loop

The for...of loop is used to iterate over elements of an array or other iterable objects.

javascript let fruits = ["Apple", "Mango", "Banana"]; for (let fruit of fruits) { console.log(fruit); }

Output:

output Apple Mango Banana

5. The for...in Loop

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

javascript let student = { name: "Rahul", age: 20, course: "Computer Science" }; for (let key in student) { console.log(key + ": " + student[key]); }

Loop Control Statements

JavaScript provides special statements to control loop execution.

  • break – Stops the loop immediately
  • continue – Skips the current iteration and moves to the next one
javascript for (let i = 1; i <= 5; i++) { if (i === 3) { continue; } console.log(i); }

Output:

output 1 2 4 5

Why Loops are Important

Loops help automate repetitive tasks and reduce the amount of code required in a program. They are commonly used in data processing, array manipulation, game development, and web applications.

Without loops, developers would have to write the same instructions multiple times, which would make programs longer and harder to maintain.

Conclusion

Loops are an essential part of JavaScript programming because they allow developers to repeat tasks efficiently. JavaScript provides several looping structures such as for, while, do...while, for...of, and for...in.

By understanding loops, developers can process data more efficiently and write cleaner programs. In the next tutorial, you will learn about Functions in JavaScript and how reusable blocks of code can simplify programming tasks.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators