Skip to content

Instantly share code, notes, and snippets.

@Nalini1998
Created January 16, 2024 07:50
Show Gist options
  • Save Nalini1998/9f97360a36ad14aa07bfc8f8f84316ba to your computer and use it in GitHub Desktop.
Save Nalini1998/9f97360a36ad14aa07bfc8f8f84316ba to your computer and use it in GitHub Desktop.
The reduce() method
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