[1, 2, 3].reduce((acc, cur) => {
acc.push(cur * 2);
return acc;
}, []);
0
What will be the output of the following JavaScript reduce code and how does it work?
kallyani
easy
0completed
6
Answer
The output will be:
[2,4,6]reduce()iterates over the array[1, 2, 3]acc(accumulator) starts as an empty array[]- On each iteration:
- The current value
curis multiplied by2 - The result is pushed into
acc - The updated
accis returned for the next iteration
- The current value
Click to Reveal Answer
Tap anywhere to see the solution
Revealed
Comments0