TalentJR

🟨 JavaScript tutorial for beginners · Class 8 · Lesson 8 of 18 · Free

Array Methods in JavaScript – map, filter and forEach

⏱ About 35 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Use forEach to do something with every item of an array
  • Create a new changed array with map and a smaller array with filter
  • Chain filter and map together and show the result on a web page

The class test is over. Ma'am wants three things: add 5 grace marks to everyone, list who passed, and print a report line for each student. With a loop you would write three loops. With map, filter and forEach, each job is one short line.

What is an arrow function? (The short way to write a function)

Array methods take a small function as their input. In Lesson 3 you wrote functions with the word function. An arrow function is a shorter way to write the same thing.

n => n * 2 means: "take n, give back n * 2". With one line, the result is returned automatically — no return word needed.

With two inputs, put them in brackets: (a, b) => a + b.

🎬 How JavaScript thinks — step 1 of 7JavaScript is about to run line 4
1function doubleOld(n) {
2 return n * 2;
3}
▶const double = n => n * 2;
5const add = (a, b) => a + b;
6console.log(doubleOld(4));
7console.log(double(4));
8console.log(add(10, 5));
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
8
8
15

How does forEach work in JavaScript?

forEach runs your function once for every item, from first to last. It is like the for…of loop from Lesson 7, written as a method.

Your function can also receive a second input: the index of the item. Handy for numbering a list.

forEach is for DOING something (printing, adding to the page). It does not give back a new array.

🎬 How JavaScript thinks — step 1 of 10JavaScript is about to run line 1
▶const names = ["Asha", "Ravi", "Meera"];
2names.forEach(name => {
3 console.log("Hello, " + name);
4});
5names.forEach((name, i) => {
6 console.log(`${i + 1}. ${name}`);
7});
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Hello, Asha
Hello, Ravi
Hello, Meera
1. Asha
2. Ravi
3. Meera

💡 Try it: add a fourth name to the array. Both lists grow by themselves.

How does map() change every item in a JavaScript array?

map runs your function on every item and collects the answers into a NEW array. The new array always has the same number of items.

The old array is not changed. That is useful: you still have the original marks if you need them.

Here map adds 5 grace marks to everyone, and also turns prices into ₹ labels.

🎬 How JavaScript thinks — step 1 of 8JavaScript is about to run line 1
▶const marks = [30, 45, 72];
2const withGrace = marks.map(m => m + 5);
3console.log("Before:", marks);
4console.log("After:", withGrace);
5const prices = [20, 35, 50];
6const labels = prices.map(p => `₹${p}`);
7console.log(labels);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Before: [30, 45, 72]
After: [35, 50, 77]
["₹20", "₹35", "₹50"]

How does filter() pick items from an array?

filter runs your function on every item. Your function answers true (keep it) or false (leave it out). filter gives back a NEW array with only the kept items.

The new array can be shorter, the same length, or even empty [ ].

Pass mark is 35 here. Change it to 50 and run again.

🟨 JavaScript — you can edit this code
Console output
Passed: [45, 72, 90]
Failed: [30, 28]
Toppers: []

How do I use filter and map together (chaining)?

filter and map both give back an array. So you can call another method straight after: first pick, then change. This is called chaining.

Read it left to right like a factory line: scores → keep the 50s → turn each into a sentence → print each one.

Quick rule: forEach = do something, map = change each item, filter = keep some items.

🟨 JavaScript — you can edit this code
Console output
🏏 Fifty-plus: 54 runs
🏏 Fifty-plus: 101 runs
🏏 Fifty-plus: 68 runs
forEach gives back: undefined

💡 forEach gives back undefined. That is why it usually comes last in a chain.

How do I show filtered JavaScript data on a web page?

Real apps use filter for search boxes and buttons like "show only…". Click the buttons below.

Each button filters the same array, then draws the result with forEach. The original array never changes.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
Showing 4 players

💡 { name: "Rohit", runs: 72 } is an object — a thing with named parts. You will learn objects fully in Lesson 10.

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

const nums = [1, 2, 3];
console.log(nums.map(n => n * 10));

Question 2. What does this print?

const nums = [4, 9, 12, 3];
console.log(nums.filter(n => n > 5));

Question 3. What does this print?

const result = [1, 2].forEach(n => n * 2);
console.log(result);

🧩 Your challenge

Cricket stats! runs = [45, 8, 102, 67, 0, 33]. 1) Use map to make every score double (a "super over" bonus) and print it. 2) Use filter to find the scores of 50 or more from the ORIGINAL runs. 3) Use forEach to print a star line for each. Expected output: Doubled: [90, 16, 204, 134, 0, 66] Big scores: [102, 67] ⭐ 102 runs ⭐ 67 runs

Expected output in the console:
Doubled: [90, 16, 204, 134, 0, 66]
Big scores: [102, 67]
⭐ 102 runs
⭐ 67 runs
🟨 JavaScript — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

const nums = [1, 2, 3];
const doubled = nums.map(n => { n * 2 });
console.log(doubled);

No error — but it prints [undefined, undefined, undefined], because nothing was returned.

✅ Right

const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2);
console.log(doubled);

With curly braces { }, an arrow function needs the word return. Either remove the braces or write { return n * 2; }.

❌ Wrong

const nums = [1, 2, 3];
const doubled = nums.forEach(n => n * 2);
console.log(doubled.length);

TypeError: Cannot read properties of undefined (reading 'length')

✅ Right

const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2);
console.log(doubled.length);

forEach gives back undefined, so doubled is undefined. Use map when you want a new array.

❌ Wrong

const marks = [100, 85, 100, 60];
const full = marks.filter(m => m = 100);
console.log(full);

No error — but every mark is kept ([100, 85, 100, 60]), because m = 100 gives 100, which counts as true.

✅ Right

const marks = [100, 85, 100, 60];
const full = marks.filter(m => m === 100);
console.log(full);

A single = puts a value in; it does not compare. Use === to check if two values are equal.

📌 Remember

  • Arrow function: n => n * 2 is a short function that returns n * 2.
  • forEach does something with every item and gives back undefined.
  • map gives a NEW array with every item changed — same length.
  • filter gives a NEW array with only the items that pass the test.
  • map and filter never change the original array.
  • Chain them: arr.filter(...).map(...).forEach(...).

Finished the quiz and the challenge?

Questions students ask

What is the difference between map and forEach in JavaScript?

map gives back a new array made from your function's answers. forEach just runs your function and gives back undefined. Use map to make a new list, and forEach to print or update the page.

Does filter change the original array?

No. filter, like map, makes a brand-new array. The original array stays exactly as it was.

Why use map and filter instead of a for loop?

A loop can do the same job, but you must create the new array and push into it yourself. map and filter do that for you in one line, so the code is shorter and easier to read.

Do I have to use arrow functions with map and filter?

No. You can pass a normal function, like nums.map(function (n) { return n * 2; }). Arrow functions are just shorter, so most people use them.