These questions ask you to predict the result of common array methods and distinguish methods with similar callbacks. Start by naming the requested result shape: transformed values, retained elements, one match, an index, a boolean, one accumulated value, or a new combined array.
Match the method to its result shape
| Method family | Procedure and small example |
|---|---|
| map and filter | map collects callback results into a new array; [2, 5].map(Number.isInteger) produces [true, true]. filter keeps original elements whose callback result is truthy; if isEven tests x % 2 === 0, [2, 5, 8].filter(isEven) produces [2, 8]. |
| find and findIndex | Stop at the first qualifying element. find returns that element or undefined; findIndex returns its position or -1. Keep element and position results separate. |
| includes and indexOf | includes reports a boolean and can match NaN through SameValueZero. indexOf reports the first position or -1 and does not match NaN when NaN is the search value. |
| some and every | some asks whether at least one element passes; every asks whether no tested element fails. On [], some is false and every is true. |
| reduce | Carry one accumulator through the elements. With an explicit initial value, start there; without one on a nonempty array, begin with the first element. If add returns sum + x, [2, 3].reduce(add, 10) produces 15. |
| forEach | Visit elements for effects, but do not collect callback returns. Even if each callback computes a value, the forEach call returns undefined. |
| concat and flat | concat creates a shallow combined array and leaves inputs unchanged. flat creates a new array and, by default, removes one nesting level rather than every level. |
The tempting wrong routes
- Expecting map to rewrite its source confuses a returned transformation with in-place mutation. Keep the original array and the new result separate.
- Giving find an index sentinel or giving findIndex undefined swaps their no-match contracts. First decide whether the method returns an element or a position.
- Assuming includes and indexOf use identical equality behavior fails for NaN. The boolean membership method can recognize it; the index search cannot.
- Requiring an element to exist before every can be true misunderstands the all-elements test. An empty input has no failing element, while some has no passing element.
- Collecting callback returns from forEach turns it into map in your mental model. forEach visits, while map builds a result array.
- Flattening every nested level by default overextends flat. Unless a deeper depth is supplied, perform exactly one layer of flattening.
Try it
Given [1, 2, 3, 4].filter(x => x > 2), which result is returned?
- [false, false, true, true]
- [3, 4]
- [1, 2]
- true
Which statements about reduce are true? Select all that apply.
- It always returns an array
- It always requires an explicit initial value
- It combines elements into one accumulated result
- A supplied initial value starts the accumulator
After result = [1].concat([2]), which statement is correct?
- result is [1, 2], and both inputs are unchanged
- the first input becomes [1, 2]
- result is the number 2
- the second input becomes empty