map, filter, reduce

Functional array methods.

Examples

map()

Creating a new array with transformed values.

const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);

function myFunction(value) {
  return value * 2;
}
console.log(numbers2); // [90, 8, 18, 32, 50]

filter()

Keeping only elements that satisfy a condition.

const ages = [32, 33, 16, 40];
const results = ages.filter(checkAdult);

function checkAdult(age) {
  return age >= 18;
}
console.log(results); // [32, 33, 40]

reduce()

Accumulating values into a single result.

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce(getSum, 0);

function getSum(total, num) {
  return total + num;
}
console.log(sum); // 10

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.