javascriptintermediate
JavaScript Array Methods
map, filter, reduce and other array higher-order methods
8 questions
By EZ4Code Team
1. What does the following code output? const arr = [1, 2, 3] console.log(arr.map(x => x * 2))
const arr = [1, 2, 3]
console.log(arr.map(x => x * 2))[2, 4, 6]
[1, 2, 3]
6
Error
Explanation: map calls the callback on each element and returns a new array without modifying the original, resulting in [2, 4, 6].
2. What does the filter method do?
Returns a new array of elements that satisfy the condition
Sorts the array
Accumulates all elements
Modifies the original array
Explanation: filter selects elements based on truthy values returned by the callback, returning a new array without modifying the original.
3. What does the following code output? const arr = [1, 2, 3, 4] console.log(arr.reduce((acc, x) => acc + x, 0))
const arr = [1, 2, 3, 4]
console.log(arr.reduce((acc, x) => acc + x, 0))10
[1,2,3,4]
4
Error
Explanation: reduce accumulates from the initial value 0: 1+2+3+4=10.
4. Which of the following methods modifies the original array?
push
map
filter
slice
Explanation: push adds elements to the end of the original array and returns the new length; map/filter/slice all return new arrays without changing the original.
5. What is the difference between find and filter?
find returns the first element that satisfies the condition, filter returns an array of all satisfying elements
They are completely identical
filter returns a single element
find returns an array
Explanation: find returns the first element satisfying the callback (or undefined if none); filter returns a new array of all satisfying elements.
6. What is the difference between some and every?
some returns true if any is truthy; every returns true only if all are truthy
They are identical
some requires all to be truthy
every requires any to be truthy
Explanation: some returns true as long as one element satisfies the condition; every requires all elements to satisfy the condition to return true.
7. What does the following code output? const arr = [1, 2, 3] console.log(arr.includes(2))
const arr = [1, 2, 3]
console.log(arr.includes(2))true
false
2
undefined
Explanation: includes checks whether the array contains the given value, returning a boolean; 2 is in the array, so it returns true.
8. What is the flatMap method equivalent to?
map followed by flat (depth 1)
flat followed by map
Only flat
Only map
Explanation: flatMap calls the callback on each element (returning an array) and flattens the result by one level, equivalent to map followed by flat(1).
More javascript Quizzes
JavaScript Basics Quiz
beginnerVariables, types, operators, functions, and core JavaScript concepts.
JavaScript ES6+ Quiz
intermediateArrow functions, destructuring, promises, modules, and modern JavaScript features.
JavaScript DOM Manipulation
intermediateDOM manipulation and event handling
JavaScript Asynchronous Programming
intermediatePromise, async/await, fetch
JavaScript Closures and Scope
advancedClosures, scope chain and hoisting