Menu
Coddy logo textTech

Array Methods Part 4

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 69 of 77.

Here are even more useful array methods:

1. map(callback): Creates a new array populated with the results of calling a provided function on every element in the calling array.

let numbers = [1, 2, 3];
let doubled = numbers.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6]

2. filter(callback): Creates a new array with all elements that pass the test implemented by the provided function.

let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]

3. reduce(callback, initialValue): Executes a reducer function on each element of the array, resulting in a single output value.

let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 10
challenge icon

Challenge

Easy

You are given an array numbers = [1, 2, 3, 4, 5]. Perform the following steps and print the results directly:

  1. Use map() to create a new array where each number is multiplied by 3, and print the result.
  2. Use filter() to create a new array that only includes numbers greater than 3, and print the result.

Cheat sheet

Array methods for transforming and filtering data:

map(callback): Creates a new array by applying a function to every element:

let numbers = [1, 2, 3];
let doubled = numbers.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6]

filter(callback): Creates a new array with elements that pass a test:

let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]

reduce(callback, initialValue): Reduces an array to a single value:

let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 10

Try it yourself

let numbers = [1, 2, 3, 4, 5];
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals