The difference between map () and filter ()?

Hi there guys, could somebody explain the difference between map () and filter () high order functions. They look very similar and do approximately or maybe exactly the same stuffs.

4 Likes

They both return a new array. map returns a new array of elements where you have applied some function on the element so that it changes the original element (typically). filter returns a new array of the elements of the original array (with no change to the original values). filter will only return elements where the function you specify returns a value of true for each element passed to the function.

So map returns the same number of elements as the original, but the element value will be transformed in some way and filter will return the same or less number of elements than the original but not change the original elements’ values.

The following MDN docs have great examples of how they are different.

10 Likes