Starlit Daily

📚 › JavaScript: make pages do things

11. Array methods: map and filter

map creates a new array by changing every item. filter keeps only the items that pass a test. Both leave the original array unchanged.

Chaining them reads like a sentence: take the even numbers, then double them.

Show example

const nums = [1, 2, 3, 4];
const evenDoubled = nums.filter(n => n % 2 === 0).map(n => n * 2);
console.log(evenDoubled.join(","));

✍️ Your turn

From [1, 2, 3, 4, 5], keep the odd numbers with filter and print them joined with commas.

← Back to course