---
title: "JavaScript Loops Explanation"  
description: "here you will learn about all types of loops in javascript with examples."  
author: "Ashutosh Patel"  
published: 2025-02-14  
updated: 2025-02-14  
canonical: https://www.mindstick.com/articles/338540/javascript-loops-explanation  
category: "javascript"  
tags: ["javascript", "javascript array", "javascript loops"]  
reading_time: 2 minutes  

---

# JavaScript Loops Explanation

Loops in [JavaScript](https://www.mindstick.com/forum/12869/how-to-c-sharp-and-javascript-email-validation-regex) **[repeat](https://www.mindstick.com/forum/156775/give-an-example-of-the-linq-repeat-method) a block of code** until a specified [condition](https://www.mindstick.com/forum/160913/writing-a-query-to-update-records-based-on-a-condition-in-sql-server) is met. They help **[automate](https://www.mindstick.com/blog/12040/how-to-automate-your-business) [repetitive tasks](https://answers.mindstick.com/qa/102118/describe-the-role-of-software-in-automating-repetitive-tasks)** and **reduce code duplication**.

#### `for` Loop

It is used when the number of iterations is known in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams).

```javascript
var numbers = [1, 3, 5, 3, 7, 8, 9];

	for(let i =0; i< numbers.length; i++)
	{
		console.log(numbers[i]);
	}
```

## [Output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular):

```plaintext
1
3
5
3
7
8
9
```

#### `while` Loop

This is used when the number of iterations is not known in advance (run when the condition is `true`).

## Example:

```javascript
var numbers = [1, 3, 5, 3, 7, 8, 9];
let i =0;
while(i <=numbers.length)
{
 console.log(numbers[i]);
 i++;
}
```

## Output:

```plaintext
1
3
5
3
7
8
9
```

#### `do...while` Loop

This ensures that the code is executed at least once, even if the condition is false.

## Example:

```javascript
var numbers = [1, 3, 5, 3, 7, 8, 9];
let i = 0;
do {
    console.log(numbers[i]);
    i++;
}
while (i <= numbers.length);
```

## Output:

```plaintext
1
3
5
3
7
8
9
```

#### `for...in` Loop (Used for Objects)

Iterates over the keys ([properties](https://www.mindstick.com/blog/673/properties)) of an object.

## Example:

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

## Output:

```plaintext
name: John
age: 30
city: New York
```

#### `for...of`Loop (Used for Arrays & Iterables)

Iterates over the [values](https://www.mindstick.com/forum/160927/sql-query-to-get-distinct-values-from-a-column-in-sql-server) ​​in an array or iterable object.

## Example:

```javascript
let numbers = [10, 20, 30, 40];
for (let num of numbers) {
    console.log(num);
}
```

## Output:

```plaintext
10
20
30
40
```

#### Loop Control Statements

- **Break** → Exits the loop immediately.
- **Continue** → Skips the current iteration and moves on to the next one.

**Use** `break`

## Example:

```javascript
for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        break;  // Stops at 3
    }
    console.log(i);
}
```

## Output:

```plaintext
1
2
```

**Use** `continue`

```javascript
for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue;  // Skips 3
    }
    console.log(i);
}
```

## Output:

```plaintext
1
2
4
5
```

I hope you understand clearly all the types of javascript loop.

Thanks.

**Also, read:** [LINQ to SQL LIKE operator with C#](https://www.mindstick.com/articles/338538/linq-to-sql-like-operator-with-c-sharp)

---

Original Source: https://www.mindstick.com/articles/338540/javascript-loops-explanation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
