Skip to content

Instantly share code, notes, and snippets.

@Nalini1998
Nalini1998 / map()
Created January 16, 2024 07:48
The map() method
The map() method is used to iterate over an array and modify its elements using a callback function. This callback function will run on each array element and return a new array of modified elements.
```
// Syntax
myArray.map(callbackFn)
myArray.map(function(element, index, array){ /* ... */ })
```
For example:
```
@Nalini1998
Nalini1998 / reduce()
Created January 16, 2024 07:50
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: