JS Built-ins
← Array148 / 226
ArrayArray.prototype.reducebeginner

Array.prototype.reduce() β€” product of all elements

πŸ“– Usage Example
const nums = [1, 2, 3, 4]
nums.reduce((acc, n) => acc * n, 1)  // β†’ 24

Use reduce() with an initial accumulator of 1 to compute the product of all elements.

reduce() with initial value 1

Use reduce() to multiply all elements together. The initial value for a product is 1 (the multiplicative identity).

[1, 2, 3, 4].reduce((acc, n) => acc * n, 1) // 24
πŸ’‘ Hints (2)
  • β€’ Use `1` as the initial value β€” using `0` would make the product always zero.
  • β€’ The reducer is `(acc, n) => acc * n`.