GyanMilega

JavaScript for Loop – In Details

Author: Sagar Kudu
Sagar Kudu

javascript for loopπŸš€ What is a for Loop?

A for loop is a control structure in JavaScript that allows you to repeat a block of code a specific number of times. It's one of the most commonly used loops in programming.

βœ… Syntax:

for (initialization; condition; increment/decrement) {
  // code to execute
}
  • Initialization: Set a variable (e.g., let i = 0)

  • Condition: Checked before each iteration (e.g., i < 5)

  • Increment: Runs after each iteration (e.g., i++)

🎯 Use when you know how many times you want to loop (like counting from 0 to 4).


πŸ“ Key Features of for Loop

Feature

Explanation

βœ… Loop control

Variables (i) control the loop flow

βœ… Block scope

Variables declared with let are scoped to the loop

βœ… Reusable

Can be used with arrays, math, functions, conditions

❌ No break or continue by default

Can be added with break or continue keywords


πŸ“š Step-by-Step Dry Run of for Loops

πŸ” Dry Run = Simulate each step of the loop execution β€” helps understand what happens under the hood.


πŸ” Example 1: Basic Loop β€” for(let i=0; i<5; i++)

for (let i = 0; i < 5; i++) {
  console.log('Hello, Sagar');
}

βœ… Dry Run (Step-by-Step)

Step

Initialization

Condition

Body

Increment

1

i = 0

0 < 5 β†’ true

Prints "Hello, Sagar"

i = 1

2

β€”

1 < 5 β†’ true

Prints "Hello, Sagar"

i = 2

3

β€”

2 < 5 β†’ true

Prints "Hello, Sagar"

i = 3

4

β€”

3 < 5 β†’ true

Prints "Hello, Sagar"

i = 4

5

β€”

4 < 5 β†’ true

Prints "Hello, Sagar"

i = 5

6

β€”

5 < 5 β†’ false

❌ Loop ends

βœ… Output:

Hello, Sagar
Hello, Sagar
Hello, Sagar
Hello, Sagar
Hello, Sagar

🚨 Total: 5 iterations (0 to 4)


πŸ” Example 2: Custom Increment β€” i = i + 1

for (let i = 2; i < 10; i = i + 1) {
  console.log(i + 2);
}

βœ… Dry Run

Step

i

Condition

Body

Increment

1

2

2 < 10 β†’ true

2+2 = 4 β†’ prints 4

i = 3

2

3

3 < 10 β†’ true

3+2 = 5 β†’ prints 5

i = 4

3

4

4 < 10 β†’ true

4+2 = 6 β†’ prints 6

i = 5

4

5

5 < 10 β†’ true

5+2 = 7 β†’ prints 7

i = 6

5

6

6 < 10 β†’ true

6+2 = 8 β†’ prints 8

i = 7

6

7

7 < 10 β†’ true

7+2 = 9 β†’ prints 9

i = 8

7

8

8 < 10 β†’ true

8+2 = 10 β†’ prints 10

i = 9

8

9

9 < 10 β†’ true

9+2 = 11 β†’ prints 11

i = 10

9

10

10 < 10 β†’ false

❌ Loop ends

βœ… Output:

4
5
6
7
8
9
10
11

🚨 Loop runs from 2 to 9 (inclusive), prints i+2


πŸ” Example 3: Step-by-2 β€” i = i + 2

for (let i = 2; i < 9; i = i + 2) {
  console.log('hello world');
}

βœ… Dry Run

Step

i

Condition

Body

1

2

2 < 9 β†’ true

prints "hello world"

2

4

4 < 9 β†’ true

prints "hello world"

3

6

6 < 9 β†’ true

prints "hello world"

4

8

8 < 9 β†’ true

prints "hello world"

5

10

10 < 9 β†’ false

❌ Loop ends

βœ… Output:

hello world
hello world
hello world
hello world

βœ… Only prints for even numbers: 2, 4, 6, 8

⚠️ Note: Stops before i = 9 (since i < 9)


πŸ” Example 4: Decrementing Loop β€” i = i - 1

for (let i = 5; i > 0; i = i - 1) {
  console.log('Hello world');
}

βœ… Dry Run

Step

i

Condition

Body

1

5

5 > 0 β†’ true

prints "Hello world"

2

4

4 > 0 β†’ true

prints "Hello world"

3

3

3 > 0 β†’ true

prints "Hello world"

4

2

2 > 0 β†’ true

prints "Hello world"

5

1

1 > 0 β†’ true

prints "Hello world"

6

0

0 > 0 β†’ false

❌ Loop ends

βœ… Output:

Hello world
Hello world
Hello world
Hello world
Hello world

βœ… 5 times β€” counts down from 5 to 1

🟑 i-- and i = i - 1 are equivalent β€” both decrement by 1


❌ Example 5: Infinite Loop (Mistake!)

for (i = 0; i > 0; i++) {
  console.log('hello world');
}

❌ Why is this wrong?

Step

i

Condition

Body

1

0

0 > 0 β†’ false

❌ Loop never starts

🚨 This loop doesn't execute β€” condition fails from the start.

🚨 If you meant for (i=0; i<=5; i++) β€” this is a different infinite loop.

βœ… Correct Version (Infinite Loop β€” Misused)

// ❌ This will cause infinite loop (if condition never fails)
for (let i = 0; i > 0; i++) {
  console.log('hello world');
}

❌ Condition i > 0 fails at start (i=0) β†’ loop never runs

βœ… To cause infinite loop:

for (let i = 0; true; i++) {
  console.log('This will never stop!');
}

⚠️ Never use for loops with true condition without break or return


βœ… Example 6: Function Inside Loop

function greet() {
  console.log("Hello, Sagar!");
}

for (let i = 0; i < 5; i++) {
  greet();
}

βœ… Dry Run

Step

i

Body

1

0

greet() β†’ prints "Hello, Sagar!"

2

1

greet() β†’ prints "Hello, Sagar!"

3

2

greet() β†’ prints "Hello, Sagar!"

4

3

greet() β†’ prints "Hello, Sagar!"

5

4

greet() β†’ prints "Hello, Sagar!"

6

5

i >= 5 β†’ loop ends

βœ… Output:

Hello, Sagar!
Hello, Sagar!
Hello, Sagar!
Hello, Sagar!
Hello, Sagar!

🎯 You can pass logic, conditions, or math inside loops.


πŸ”„ Example 7: While Loop (Alternative)

let i = 0;

while (i < 5) {
  console.log('Hello World');
  i++;
}

βœ… Dry Run

Step

i

Condition

Body

1

0

0 < 5 β†’ true

prints "Hello World", i = 1

2

1

1 < 5 β†’ true

prints, i = 2

3

2

2 < 5 β†’ true

prints, i = 3

4

3

3 < 5 β†’ true

prints, i = 4

5

4

4 < 5 β†’ true

prints, i = 5

6

5

5 < 5 β†’ false

❌ Loop ends

βœ… Output:

Hello World
Hello World
Hello World
Hello World
Hello World

βœ… while loop is useful when you don't know the number of iterations in advance.


πŸ“¦ Example 8: for Loop with Array

let arr = [2, 2, 3, 5, 6, 7, 8, 9, 10, 11];

console.log(arr.length); // 10
console.log(arr[0]);     // 2
console.log(arr[1]);     // 2
console.log(arr[2]);     // 3
console.log(arr[3]);     // 5
console.log(arr[4]);     // 6
console.log(arr[5]);     // 7
console.log(arr[6]);     // 8
console.log(arr[7]);     // 9
console.log(arr[8]);     // 10
console.log(arr[9]);     // 11

βœ… Use Loop to Access All Elements

console.log('-------------------------');
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

βœ… Dry Run

Step

i

arr[i]

1

0

2

2

1

2

3

2

3

4

3

5

5

4

6

6

5

7

7

6

8

8

7

9

9

8

10

10

9

11

βœ… Output:

2
2
3
5
6
7
8
9
10
11

πŸš€ Alternative to manually writing indices β€” much cleaner and scalable!


πŸ” Filter: Print Only Even Numbers

console.log('------------Even Numbers--------------');
for (let i = 0; i < arr.length; i++) {
  if (arr[i] % 2 == 0) {
    console.log(arr[i]);
  }
}

βœ… Output:

2
2
6
8
10

🎯 Even numbers: divisible by 2 β†’ remainder 0


πŸ” Filter: Print Only Odd Numbers

console.log('------------Odd Numbers--------------');
for (let i = 0; i < arr.length; i++) {
  if (arr[i] % 2 == 1) {
    console.log(arr[i]);
  }
}

βœ… Output:

3
5
7
9
11

🎯 Odd numbers: remainder 1 when divided by 2


πŸ“Œ Summary Table

Loop Type

Use Case

Example

i < 5

Counting 0 to 4

Basic loop

i < 9, step 2

Step over even numbers

i = 2, 4, 6, 8

i > 0, decrement

Count down

5 β†’ 1

i > 0, i++

Invalid (infinite)

Avoid!

array

Access each element

for (i=0; i<arr.length; i++)

condition

Filter even/odd

arr[i] % 2 == 0


🚨 Common Mistakes & Fixes

Mistake

Fix

for (i=0; i>0; i++)

Use i < 5 or i <= 5

Forgetting let

Use let i for block scope

Incorrect array index

Use i < arr.length

Infinite loop

Add break or return


πŸ” SEO Keywords (for better search visibility)

  • JavaScript for loop tutorial

  • How to use for loop in JavaScript

  • for loop with array in JavaScript

  • for loop dry run example

  • JavaScript loop with conditions

  • print even numbers in array JavaScript

  • for loop with function

  • while vs for loop in JavaScript

  • JavaScript loop beginners guide

  • JavaScript array loop example


πŸ“š Final Tips

  • Use let for loop variables β€” avoids hoisting issues

  • Always test your loops with dry runs

  • Use i < arr.length for arrays

  • Filter data with if conditions inside loops

  • Avoid infinite loops β€” always check the condition


πŸ“Œ Ready for coding interviews? Practice these loops with arrays, conditions, and functions. This guide will help you understand, debug, and explain for loops confidently.

πŸ’‘ Next Step: Learn about for...of loops, forEach(), and map() for more advanced array iteration.

Powered by wisp

Related Posts

JavaScript Loops Explained with Examples | For Loop

Learn how loops work in JavaScript with beginner-friendly examples. This complete guide covers for loops, while loops, and practical use cases like printing messages, iterating arrays, and filtering even/odd numbers.

Read Full Story

Introduction to Node.js (Full Interview Guide)

Explore Node.js with our concise infographic detailing its core functionalities. Learn how this JavaScript runtime powers fast, scalable backend development, real-time apps, and robust APIs. Perfect for developers and businesses looking to understand Node.js benefits.

Read Full Story

What is a Function in JavaScript? | Function Examples & Guide

Functions in a JavaScript. Learn what functions are in JavaScript with examples. This beginner-friendly guide covers function declarations, parameters, return values, and more.

Read Full Story

πŸ“˜ TypeScript: Day 2 – Interfaces & Object Types

Let us consider a previous example: const car: { brand: string; year: number } = { brand: "Tata", year: 2025 }; That works fine, but imagine you have lots of objects with the same shape. Typing { brand: string; year: number } again and again is messy. πŸ‘‰ This is where interfaces come in.

Read Full Story
Loading...
Β© Gyan Milega 2025