How does the initialValue function work in a javascript reduction function?

I am sure that I am reading this incorrectly, but MDN is talking about it ...

InitialValue
The value to use as the first argument to the first callback. If the original value is not specified, the first element of the array will be used. Calling the reduce () method on an empty array without an initial value is an error.

then it says ... Doesn't that say different things? Thank!

If initialValue is not provided, the reduce () function will execute the callback function, starting at index 1, skipping the first index. If initialValue is provided, it will start at index 0.

+4
source share
2 answers

I agree a little with the wording.

:

,

initialValue , reduce() , 1,

, :

  • ,
  • ,

?

, . 1, 0 , .

+8

, . , . .reduce() , http://javascript.info. , .

, StackOverflow.

, currentValue . TypeError, initialValue .

: , JavaScripts Reduce, , , freeCodeCamp .

+3

Source: https://habr.com/ru/post/1692287/


All Articles