InterviewBuddy
All interview questions

What is the difference between map, filter, and reduce?

Beginner

Answer

map transforms each item, filter keeps items that pass a test, and reduce combines items into one accumulated result. All three iterate over an array.

Explanation

Choose the operation that communicates the intent. A complex reduce can be harder to read than a small loop.

Example

items.filter(x => x.active).map(x => x.name)