Member-only story
JavaScript Interview Prep: map(), filter() and reduce()
2 min readJun 20, 2024
In Javascript, the map(), filter() and reduce() methods are powerful array functions that allow you to transform and filter arrays easily. They are all higher order functions becaues they take user-defined functions as parameters.
JavaScript Array Map()
- The map() method in JavaScript is used to create a new array by applying a given function to each element of the original array.
const numbers = [5, 10, 15, 20, 25, 30];
const multipliedNumbers =
numbers.map(num => num * 3);
console.log(multipliedNumbers)
//output: [ 15, 30, 45, 60, 75, 90 ]
- map() creates a new array with the same length as the original array, but with each element transformed by the callback function
- map() is used when you want to transform each element in an array
- map() returns a new array with the same length as the original array
JavaScript Array Filter()
- filter() is used to create a new array that includes only the elements from an existing array that pass a specified condition
const numbers = [5, 10, 15, 20, 25, 30];
const numbersGreaterThan20 =
numbers.filter(num => num > 20);
console.log(numbersGreaterThan20)…