Namaste JavaScript — Interview Quick Notes
Ep 18–19: Higher-Order Functions, map / filter / reduce
- A Higher-Order Function takes a function as an argument or returns one.
- map — transforms each element, returns a new array of the same length.
- filter — returns a new array with elements where the callback returns true.
- reduce — accumulates all elements into a single output value.
- reduce can replicate map and filter — useful to know for interviews.
1const nums = [1, 2, 3, 4]
2nums.map((x) => x * 2) // [2, 4, 6, 8]
3nums.filter((x) => x % 2 === 0) // [2, 4]
4nums.reduce((acc, x) => acc + x, 0) // 10