Created
January 16, 2024 07:50
-
-
Save Nalini1998/9f97360a36ad14aa07bfc8f8f84316ba to your computer and use it in GitHub Desktop.
The reduce() method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is one of the most powerful JavaScript array methods because it applies a reducer function to each element of your array and returns a single output. The reducer function iterates through all elements in your array from left to right (in order) and returns the result of the previous element’s calculation. | |
Thus, the final result is a single value. This single value is the function’s accumulated result after executing a reducer function for every array element. | |
``` | |
// Syntax | |
myArray.reduce(callbackFn, initialValue) | |
``` | |
For example: | |
``` | |
let salaries = [100, 300, 500]; | |
let salary = salaries.reduce((x, y) => x + y, 0); | |
console.log(salary); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment