0% found this document useful (0 votes)
12 views1 page

Understanding JavaScript reduce() Method

The reduce() method in JavaScript is used to iterate over an array and reduce it to a single value, such as a sum or product. It takes a callback function and an optional initial value for the accumulator. An example demonstrates summing an array of numbers, resulting in an output of 15.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Understanding JavaScript reduce() Method

The reduce() method in JavaScript is used to iterate over an array and reduce it to a single value, such as a sum or product. It takes a callback function and an optional initial value for the accumulator. An example demonstrates summing an array of numbers, resulting in an output of 15.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

reduce() in JavaScript

The reduce() method is used to iterate over an array and reduce it to a single
value. This value can be a sum, product, object, or even another array.

Syntax

[Link](callback, initialValue)
Parameters
callback(accumulator, currentValue, index, array)

accumulator → Stores the accumulated result.


currentValue → The current element being processed.
index (optional) → The current index.
array (optional) → The original array.
initialValue (optional)

The initial value of the accumulator.


If not provided, the first element is used as the initial value.
Example 1: Sum of an Array

const numbers = [1, 2, 3, 4, 5];

const sum = [Link]((acc, num) => acc + num, 0);

[Link](sum); // Output: 15

You might also like