Ugrás a fő tartalomra
A Frontend ingyenes bónusz a CodeMentor AI-n — a fő termék a Python. Regisztrálj a haladásod mentéséhez és nyisd meg mind a 299 Frontend leckét + 1,100+ Python leckét.Regisztrálj — ingyen
← ⚡ JavaScript & the browser·Modul C1 · Lecke 4
FeladatStart with const nums = [1, 2, 3, 4, 5]. Use map to make doubled (each element × 2). Use filter to make evens from nums (% 2 === 0). Log doubled then evens.

Tömbök: push, map, filter – a három, amit naponta használ

100 XP8 minFREE
Elmélet

Three array methods cover 80% of JS work

const nums = [1, 2, 3];

nums.push(4);                          // mutates → [1, 2, 3, 4]
const doubled = nums.map(n => n * 2);  // returns NEW array → [2, 4, 6, 8]
const evens = nums.filter(n => n % 2 === 0);  // → [2, 4]

push adds to the end and returns the new length. It mutates the original array — bear that in mind when sharing data.

map runs your function on every item and returns a new array of the same length. The original is untouched.

filter runs your function on every item and returns a new array containing only items where the function returned a truthy value.

The "returns a new array" pattern

map and filter never mutate. You can chain them, save results to const variables, pass them around. That makes them safe to use in React, in worker threads, and across modules.

const active = users
  .filter(u => u.active)
  .map(u => u.email);

Reads top-to-bottom like a sentence. This is one of the things that separates "writes JavaScript" from "writes good JavaScript."

When to use which

  • push — when you're explicitly building up a list step by step. Loops, accumulators.
  • map — same length, transformed items.
  • filter — same items, fewer of them.

Don't reach for for loops in 2026 unless you genuinely need the index. The named methods read better and reviewers prefer them.

2 lapok
Élő előnézet
console output appears here…
← ElőzőKövetkező lecke →

Egy Python- vagy webes tipp naponta — emailben

Rövid, kézzel írt, spam nélkül. Egy kattintással leiratkozhatsz.