0

What will be the output of the following JavaScript reduce code and how does it work?

author
kallyani
easy
0
6
[1, 2, 3].reduce((acc, cur) => {
acc.push(cur * 2);
return acc;
}, []);
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 cur is multiplied by 2
    • The result is pushed into acc
    • The updated acc is returned for the next iteration

Click to Reveal Answer

Tap anywhere to see the solution

Revealed

Comments0
    What will be the output of the following JavaScript reduce code and how does it work? - reduce, array | EBAT