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
- Step 1: Initialization β
let i = 0executes once. - Step 2: Condition Check β JavaScript tests
i < 5. Iffalse, exit loop. - Step 3: Body Execution β The statements inside
{ ... }run. - 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 variableito 0 in block scope.i < 3: Evaluates ifiis strictly less than 3.console.log(...): Logs current iteration count.i++: Increasesiby 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
- Beginner: Write a
forloop printing even numbers from 2 to 10. - Intermediate: Write a
forloop that reverses an array without using the built-in.reverse()method. - Advanced: Create a
forloop that checks if a given integer is a prime number.