Array Reduce | Learn JavaScript Array Reduce In 10 Minutes #javascript #javascript_tutorial



Array Reduce | Learn JavaScript Array Reduce In 10 Minutes #javascript #javascript_tutorial

Array Reduce | Learn JavaScript Array Reduce In 10 Minutes #javascript #javascript_tutorial

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, to pass in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

callbackFn
A function to execute for each element in the array. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. For the last invocation, the return value becomes the return value of reduce().

The function is called with the following arguments:

accumulator
The value resulting from the previous call to callbackFn. On first call, initialValue if specified, otherwise the value of array[0].

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

currentIndex
The index position of currentValue in the array. On first call, 0 if initialValue was specified, otherwise 1.

array
The array reduce() was called upon.

initialValue Optional
A value to which accumulator is initialized the first time the callback is called. If initialValue is specified, callbackFn starts executing with the first value in the array as currentValue. If initialValue is not specified, accumulator is initialized to the first value in the array, and callbackFn starts executing with the second value in the array as currentValue. In this case, if the array is empty (so that there’s no first value to return as accumulator), an error is thrown.

link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

#javascript #javascriptengineer #javascript_tutorial #frontend #array #viralvideo

Javascript, web development, web developer, front end, front-end, es6, learn javascript, javascript programming, programming, array, reduce,

Comments are closed.