DEV Community

Discussion on: Array cheatsheet Javascript

Collapse
 
shamolmojumder profile image
Shamol Mojumder

confusion with filter & map

Collapse
 
vishalraj82 profile image
Vishal Raj

@shamol
Assuming that an array has n elements, the filter function can return 0 to n elements, based on the condition. Such as
const even = [1,2,3,4,5,6].filter(n => n %2 === 0); // returns [2, 4, 6];

Assuming that an array has n elements, the map function will run every array element through the function and return n elements.
const double = [1,2,3,4,5].map(n => n * 2); // returns [2, 4, 6, 8, 10];

Hope this clears.