JavaScript Tutorial

JavaScript Array Iteration

forEach, map, filter, and find walk a list. map returns a new array. forEach does not.

Walk every item

Iteration means visiting each value in order. You can do that with a for loop and an index. Array methods take a function instead: JavaScript calls that function once per item, with the item as the first argument.

The function you pass in is a callback. Write it with the function keyword. Later chapters cover the short arrow form. Both work here.

Run these in /javascript/try. Log the return value of each method. That is how you see that forEach is not map.

forEach runs a function

list.forEach(callback) visits every item. The callback can log, add to a total, or write to the page. forEach itself returns undefined. Do not assign its result to a new list — there is no new list.

Example

const names = ["Ada", "Grace", "Linus"];
const lines = [];

names.forEach(function (name) {
  console.log(name);
  lines.push(name);
});

console.log(names.forEach(function () {}));
document.body.textContent = lines.join(", ");

map returns a new array

list.map(callback) builds a new array from the values your callback returns. The original list is not mutated. Forget return and every slot in the new array isundefined.

Example

const prices = [4, 10, 7];
const doubled = prices.map(function (n) {
  return n * 2;
});

console.log(prices);
console.log(doubled);

document.body.textContent =
  "original " + prices.join(", ") + " / doubled " + doubled.join(", ");

filter keeps some items

list.filter(callback) returns a new array of the items for which the callback returned a truthy value. The original is unchanged. Use it to drop zeros, hide completed tasks, or keep scores at or above a cutoff.

Example

const scores = [88, 41, 95, 70];
const passed = scores.filter(function (n) {
  return n >= 50;
});

console.log(passed);
console.log(scores);

document.body.textContent = "passed: " + passed.join(", ");

find returns one item

list.find(callback) returns the first item that matches, or undefined if none match. It does not return an array. findIndex returns the index, or -1.

Stop there when you need one record. Filtering the whole list and then taking index 0 still walks every item; find can stop early.

Example

const tasks = ["draft", "review", "ship"];
const hit = tasks.find(function (word) {
  return word === "review";
});
const missing = tasks.find(function (word) {
  return word === "archive";
});

console.log(hit);
console.log(missing);

document.body.textContent = String(hit) + " / " + String(missing);

Pick the method that matches the job

MethodReturnsMutates the list?
forEachundefinedNo (the callback might)
mapA new array of the same lengthNo
filterA new array, maybe shorterNo
findOne item or undefinedNo

A classic for loop is still the right tool when you need the index, an earlybreak, or a running total you want to write by hand. The next flow chapters cover that loop.

What to remember

  • forEach runs a function. It does not return a list.
  • map returns a new array. Return a value from the callback.
  • filter keeps items whose callback is truthy.
  • find returns the first match, not an array.

Next: dates — the clock on the machine, year, month, and a stamp you format yourself.

FAQ: JavaScript Array Iteration

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 array iteration 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 array iteration in this JavaScript JavaScript lesson (JavaScript Array Iteration).

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.