Coding/Learn/javascript/for loops
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnStandard For Loops & Iteration
11 / 20 0%
Lesson 11 of 20
5 mins read

Standard For Loops & Iteration

PreviousNext Lesson
In Plain English

The classic for loop consists of three expressions: initialization, condition test, and increment/step update. It provides precise, index-based iteration over numbers and collections.

The Mental Model

"A for loop is like a lap counter on a racetrack: you start at lap 1, check if you have completed 10 laps, and increment the counter at the finish line of every lap."

Deep Dive: How It Works

3-Part Header: for (let i = 0; i < limit; i++) { ... }

Scoped Index Variable: Using let i scopes the variable strictly to the loop body.

Step Control: You can increment by any step (e.g., i += 2) or decrement (i--).

Syntax Blueprint

for (initialization; condition; update) { // body }

Initialization runs once, condition is checked before each turn, and update runs after each turn.

PointCore Rules to Remember

Point star
Index Tracking: Provides direct access to the loop index counter i.
Point star
Block Scoping: let i is re-bound for every iteration, making it safe for closures.
Point star
Reversibility: Easy to loop backwards: for (let i = n - 1; i >= 0; i--).

Common Beginner Traps & How to Fix Them

Mistake:Off-by-one errors with <= vs <.

Why it happens: For a 5-item list, looping with i <= 5 attempts to read index 5, which is out of bounds.

How to fix: Use i < array.length for zero-indexed array boundaries.

Pro Tip: Keep loop header expressions pure and avoid calling expensive functions inside the condition check repeatedly.

Live Interactive Example

Hit Run Code to see it live
Counting and Accumulating with For Loops
JavaScript ES2024
1
2
3
4
5
6
7
8
Output Console
Click "Run Code" to view the rendered output.
How it works: The loop adds 1+2+3+4+5 sequentially to calculate 15.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate product with a for loop

  • PointCalculate factorial of 4 (1 * 2 * 3 * 4 = 24).
  • PointInitialize let product = 1;
  • PointUse a for loop from i = 1 to i <= 4, multiplying product *= i.
  • PointPrint "Product: 24".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates