JavaScript for Loop β In Details
π 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 (  | 
β Block scope  | Variables declared with   | 
β Reusable  | Can be used with arrays, math, functions, conditions  | 
β No   | Can be added with   | 
π 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  | 
  | 
  | Prints   | 
  | 
2  | β  | 
  | Prints   | 
  | 
3  | β  | 
  | Prints   | 
  | 
4  | β  | 
  | Prints   | 
  | 
5  | β  | 
  | Prints   | 
  | 
6  | β  | 
  | β 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  | 3  | 
  | 
  | 
  | 
3  | 4  | 
  | 
  | 
  | 
4  | 5  | 
  | 
  | 
  | 
5  | 6  | 
  | 
  | 
  | 
6  | 7  | 
  | 
  | 
  | 
7  | 8  | 
  | 
  | 
  | 
8  | 9  | 
  | 
  | 
  | 
9  | 10  | 
  | β 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  | 
  | prints "hello world"  | 
2  | 4  | 
  | prints "hello world"  | 
3  | 6  | 
  | prints "hello world"  | 
4  | 8  | 
  | prints "hello world"  | 
5  | 10  | 
  | β 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(sincei < 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  | 
  | prints "Hello world"  | 
2  | 4  | 
  | prints "Hello world"  | 
3  | 3  | 
  | prints "Hello world"  | 
4  | 2  | 
  | prints "Hello world"  | 
5  | 1  | 
  | prints "Hello world"  | 
6  | 0  | 
  | β Loop ends  | 
β Output:
Hello world
Hello world
Hello world
Hello world
Hello world
β 5 times β counts down from 5 to 1
π‘
i--andi = i - 1are 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  | 
  | β 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 > 0fails 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
forloops withtruecondition withoutbreakorreturn
β 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  | 
  | 
2  | 1  | 
  | 
3  | 2  | 
  | 
4  | 3  | 
  | 
5  | 4  | 
  | 
6  | 5  | 
  | 
β 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  | 
  | prints "Hello World",   | 
2  | 1  | 
  | prints,   | 
3  | 2  | 
  | prints,   | 
4  | 3  | 
  | prints,   | 
5  | 4  | 
  | prints,   | 
6  | 5  | 
  | β Loop ends  | 
β Output:
Hello World
Hello World
Hello World
Hello World
Hello World
β
whileloop 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  | 
|---|---|---|
  | Counting 0 to 4  | Basic loop  | 
  | Step over even numbers  | 
  | 
  | Count down  | 5 β 1  | 
  | Invalid (infinite)  | Avoid!  | 
  | Access each element  | 
  | 
  | Filter even/odd  | 
  | 
π¨ Common Mistakes & Fixes
Mistake  | Fix  | 
|---|---|
  | Use   | 
Forgetting   | Use   | 
Incorrect array index  | Use   | 
Infinite loop  | Add   | 
π 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
letfor loop variables β avoids hoisting issuesAlways test your loops with dry runs
Use
i < arr.lengthfor arraysFilter data with
ifconditions inside loopsAvoid 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
forloops confidently.
π‘ Next Step: Learn about
for...ofloops,forEach(), andmap()for more advanced array iteration.
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 StoryIntroduction 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 StoryWhat 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