The for Loop in JavaScript

🟒 Beginner Level | Counter Iteration

πŸ“– Definition

The for loop repeats a block of code a specific number of times using an initialization variable, a termination condition, and a step increment expression.

πŸ€” Purpose

When you know in advance how many times an operation must repeatβ€”such as processing all elements in an array of size $N$β€”a for loop provides a concise counter-driven control structure.

🌐 Multilingual Explanations

English

A for loop consists of three expressions enclosed in parentheses: for (initialization; condition; increment). The initialization runs once, the condition checks before every iteration, and the increment executes after every loop body completion.

Hindi

for loop teen parts se banta hai: let i = 0 (initialization), i < 5 (condition test), aur i++ (update). Code body evaluate hone ke baad counter automatically increment hota hai.

Marathi

for loop cha wapar jenvha iteration chi sankhya mahit aste tenvha kela jato. Loop cha counter i++ mule dar iteration nantar vadhla jato.

Hinglish

Count-based repetition ke liye for (let i = 0; i < n; i++) use karein. Index-based array traversal ke liye step-by-step execution follow hota hai.

πŸ“ Syntax & Parameters

for (initialization; condition; increment) {
  // Code executed on each iteration
}
  • initialization: Executed once before loop starts (e.g. let i = 0).
  • condition: Evaluated before each iteration. If false, loop terminates.
  • increment: Executed after each iteration body (e.g. i++).

⚑ Execution Flow

  1. Step 1: Initialization β€” let i = 0 executes once.
  2. Step 2: Condition Check β€” JavaScript tests i < 5. If false, exit loop.
  3. Step 3: Body Execution β€” The statements inside { ... } run.
  4. Step 4: Increment β€” i++ runs, increasing counter by 1. Repeat from Step 2.

πŸ’‘ Progressive Examples

Example 1: Basic Counter

for (let i = 0; i < 3; i++) {
  console.log("Iteration count: " + i);
}

Example 2: Array Index Traversal

let fruits = ["Apple", "Banana", "Cherry"];

for (let i = 0; i < fruits.length; i++) {
  console.log("Fruit at index " + i + ": " + fruits[i]);
}

Example 3: Nested Loops (Grid Generation)

for (let row = 1; row <= 2; row++) {
  for (let col = 1; col <= 2; col++) {
    console.log("Cell (" + row + ", " + col + ")");
  }
}

πŸ–₯️ Expected Output

Iteration count: 0
Iteration count: 1
Iteration count: 2
Fruit at index 0: Apple
Fruit at index 1: Banana
Fruit at index 2: Cherry
Cell (1, 1)
Cell (1, 2)
Cell (2, 1)
Cell (2, 2)

πŸ” Line-by-Line Code Breakdown

  • let i = 0: Initializes counter variable i to 0 in block scope.
  • i < 3: Evaluates if i is strictly less than 3.
  • console.log(...): Logs current iteration count.
  • i++: Increases i by 1 after each loop body execution.

⚠️ Variations & Edge Cases

If the termination condition is initially false (e.g. i < 0 when i = 0), the loop body executes zero times!

❌ Common Beginner Mistakes

Infinite Loops & Off-by-One Errors: Forgetting to increment the loop variable (e.g. omitting i++) causes an infinite loop that freezes the browser tab. Using i <= array.length leads to an undefined out-of-bounds index access.

πŸ› οΈ Real-World Practical Usage

Batch processing data records:

let transactions = [100, 250, 75];
let total = 0;

for (let i = 0; i < transactions.length; i++) {
  total += transactions[i];
}
console.log("Total Revenue: $" + total);

🎯 Tiered Practice Exercises

  1. Beginner: Write a for loop printing even numbers from 2 to 10.
  2. Intermediate: Write a for loop that reverses an array without using the built-in .reverse() method.
  3. Advanced: Create a for loop that checks if a given integer is a prime number.