JavaScript Tutorial

JavaScript For

for (start; test; step) repeats. for...of walks values. for...in walks keys — usually not for arrays.

Three slots in one line

A classic for loop has three parts separated by semicolons: start, test, step. The start runs once. Then JavaScript checks the test, runs the body, runs the step, and checks again, until the test is false.

Use this form when you know how many passes you want, or when you need the index as well as the value.

Run the loops in /javascript/try. If the page never finishes, the test never became false. Stop, fix the step, run again.

Count with a classic for

let i = 0 starts the counter. i < 5 is the test. i = i + 1 is the step. The values printed are 0, 1, 2, 3, 4 — five times, not six.

Example

const lines = [];

for (let i = 0; i < 5; i = i + 1) {
  console.log(i);
  lines.push(String(i));
}

document.body.textContent = lines.join(", ");
SlotThis exampleWhen it runs
Startlet i = 0Once, before the first check
Testi < 5Before every pass, including the first
Stepi = i + 1After each pass, before the next check

i++ means the same as i = i + 1 here. The let in the start slot is scoped to the loop. After the closing brace, i is gone.

Walk an array by index

The test is usually i < list.length. Then list[i] is the current item. This is the form you want when the index itself matters — pairing two lists, writing “item 3 of 10”, or splicing while you walk (carefully).

Example

const colors = ["red", "green", "blue"];
const lines = [];

for (let i = 0; i < colors.length; i = i + 1) {
  const row = i + ": " + colors[i];
  console.log(row);
  lines.push(row);
}

document.body.textContent = lines.join(" | ");

for...of walks values

for (const item of list) gives you each value, in order, with no index. That is the loop you want when you only care about the item. It works on arrays and on other iterables such as strings.

Example

const colors = ["red", "green", "blue"];
const lines = [];

for (const color of colors) {
  console.log(color);
  lines.push(color);
}

document.body.textContent = lines.join(", ");

for...in walks keys

for (const key in object) visits enumerable property names. On an array those names are the index strings "0", "1", … plus any extra properties someone hung on the array. That is why this tutorial does not use for...in to walk arrays.

Example

const person = { name: "Ada", role: "engineer" };
const lines = [];

for (const key in person) {
  const row = key + "=" + person[key];
  console.log(row);
  lines.push(row);
}

document.body.textContent = lines.join(", ");

Need the values of an array? Use for...of or a classic index loop. Savefor...in for plain objects.

Which loop to write

LoopGives youTypical use
for (;;)An index you controlCount, or list[i]
for...ofEach valueWalk an array
for...inEach keyWalk a plain object

What to remember

  • Classic for: start, test, step. Counting from 0 is the usual habit.
  • for...of is the straightforward array walk.
  • for...in is for object keys, not for arrays.
  • The test must become false. A missing step is an infinite loop.

Next: while and do...while — repeat while a test stays true.

FAQ: JavaScript For

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript for loop examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript for loop in this JavaScript JavaScript lesson (JavaScript For).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.