Skip to main content

Command Palette

Search for a command to run...

JS reduce

Updated
•1 min read•View as Markdown
JS reduce
  1. Returns single value.
  2. Doesn't mutate original array.
  3. Doesn't execute the function for empty array elements.
  4. Syntax: array.reduce(function(previousValue, currentValue, currentIndex, arr), initialValue);

reduce1.jpeg

previousValue: value resulting from the previous call to callBackFn. On first call, initialValue if specified otherwise array[0].

currentValue: value of the current element.On the first call, the value of array[0] if initialValue specified otherwise array[1].

currentIndex: currentValue index. On first call it is 0 if initialValue specified otherwise 1.

reduce2.jpeg

Examples

reduce3.jpeg


reduce4.jpeg


reduce5.jpeg


Functional Composition:

reduce6.jpeg

In above, example input is function returned by (...functions) as we are calling multiply6(6). Hence input is 6.

Reference: MDN official

Thank you. :)