🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

for Loop

AI & DATA SCIENCE // for

The classic for loop runs a block a specific number of times using an initializer, condition, and update expression.

Syntax

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

Deep Dive Course

The **for** loop has three parts: **initialization** (runs once before the loop), **condition** (checked before each iteration), and **update** (runs after each iteration). If condition is false initially, the body never runs. All three parts are optional.

1Understanding for Loop

The for loop has three parts: initialization (runs once before the loop), condition (checked before each iteration), and update (runs after each iteration). If condition is false initially, the body never runs. All three parts are optional.

💡

Cache array.length in a variable when iterating large arrays to avoid recalculating it each iteration.

editor.html
// Sum of numbers 1-100
let sum = 0;
for (let i = 1; i <= 100; i++) {
  sum += i;
}
console.log(sum); // 5050 (Gauss formula)

// Iterate backwards
const arr = ['a', 'b', 'c'];
for (let i = arr.length - 1; i >= 0; i--) {
  console.log(arr[i]);
}
localhost:3000

2Practical Example

Here is a real-world application of for Loop showing how it is used in production JavaScript code.

editor.html
// Nested loops: multiplication table
for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    process.stdout.write((i * j) + '\t');
  }
  console.log();
}
localhost:3000

3Best Practices

Follow these guidelines when working with for Loop:

1. Use let for loop counter to keep it block-scoped

2. Prefer for...of when you don't need the index

3. Break early with break when the target is found

⚠️

Tip: Cache array.length in a variable when iterating large arrays to avoid recalculating it each iteration.

editor.html
// Sum of numbers 1-100
let sum = 0;
for (let i = 1; i <= 100; i++) {
  sum += i;
}
console.log(sum); // 5050 (Gauss formula)

// Iterate backwards
const arr = ['a', 'b', 'c'];
for (let i = arr.length - 1; i >= 0; i--) {
  console.log(arr[i]);
}
localhost:3000

Examples

Example 01Basic Usage
// Sum of numbers 1-100
let sum = 0;
for (let i = 1; i <= 100; i++) {
  sum += i;
}
console.log(sum); // 5050 (Gauss formula)

// Iterate backwards
const arr = ['a', 'b', 'c'];
for (let i = arr.length - 1; i >= 0; i--) {
  console.log(arr[i]);
}
Example 02Advanced Example
// Nested loops: multiplication table
for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    process.stdout.write((i * j) + '\t');
  }
  console.log();
}

Best Practices

  • Use let for loop counter to keep it block-scoped
  • Prefer for...of when you don't need the index
  • Break early with break when the target is found

Interview Question

How would you iterate over an array and get both index and value?

Hint: Classic for or Array.entries().

Two ways: classic for loop (for let i=0; i<arr.length; i++) gives arr[i] and i. Or use for...of with entries(): for (const [i, val] of arr.entries()). The entries() approach is cleaner and avoids off-by-one errors.

Exercises

MediumPractice using for Loop in a real scenario.
View Solution
// Sum of numbers 1-100
let sum = 0;
for (let i = 1; i <= 100; i++) {
  sum += i;
}
console.log(sum); // 5050 (Gauss formula)

// Iterate backwards
const arr = ['a', 'b', 'c'];
for (let i = arr.length - 1; i >= 0; i--) {
  console.log(arr[i]);
}

Frequently Asked Questions

How would you iterate over an array and get both index and value?

Two ways: classic for loop (for let i=0; i

Related Functions

loopswhilebreakcontinuefor-of