GUIDEDeep dive
Reading map, filter and reduce
map returns a new array of the same length, filter returns a subset, and reduce returns one accumulated value. Read callback arguments (value, index, array), then the return value; chains run left to right.
Three methods
map: transform each element into a new value.
filter: keep elements whose callback is true.
reduce: combine into one value with an accumulator.
Chaining
[1,2,3].map(x=>x*2).filter(x=>x>2) -> [4,6].
sort mutates in place; slice does not.
FAQ
Does map mutate?
No; it returns a new array.
Does reduce's initial value matter?
Yes; without it the first item is the initial value, and an empty array throws.
Sources
Related calculators
JavaScript Output: 1000 Gamified Expression and Array Cards1000 JavaScript output cards covering arithmetic, strings, array methods, objects, JSON and logic, in 50 levels with spaced repetition.More guides
Hiragana vs KatakanaRead guideSpaced Repetition and the Forgetting CurveRead guideOn Readings vs Kun ReadingsRead guideAn N5 Kanji Study OrderRead guideKEEP READING
Keep reading
Read in order, or jump to another guide on the same topic.
← PreviousHow to Read JavaScript OutputOperators, coercion, truthiness and JSON formatting.Next →Regex Basics: Classes, Quantifiers, Anchors\d, \w, +, *, ?, ^, $ and escaping.