You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Example: Calculate the sum of all numbers in an array.
87
+
88
+
const numbers = [1, 2, 3, 4, 5];
89
+
90
+
const sum = numbers.reduce(function(accumulator, currentValue) {
91
+
return accumulator + currentValue;
92
+
}, 0);
93
+
94
+
console.log(sum);
95
+
96
+
Does it mutate the source or not: The reduce() method does not mutate the source array. It performs calculations on the elements of the array and returns a single value.
97
+
98
+
Argument List and Meaning:
99
+
100
+
callback (required): The function to execute on each element. It takes four arguments:
101
+
102
+
accumulator: The accumulated value computed by previous iterations or the initial value provided.
103
+
currentValue: The current element being processed in the array.
104
+
index (optional): The index of the current element being processed.
105
+
array (optional): The array that reduce() is being applied to.
106
+
initialValue (optional): The initial value for the accumulator. If not provided, the first element of the array is used as the initial value.
107
+
What it returns: The reduce() method returns a single value that results from the accumulation of values computed by the callback function.
Does it mutate the source or not: The slice() method does not mutate the source array. It returns a new array containing the extracted portion of the original array.
124
+
125
+
Argument List and Meaning:
126
+
127
+
start (optional): The index at which to begin extraction. If not specified, slice() starts from index 0.
128
+
end (optional): The index at which to end extraction. The extracted portion does not include the element at the end index. If not specified, slice() extracts all elements from the start index to the end of the array.
129
+
What it returns: The slice() method returns a new array containing the extracted portion of the original array.
130
+
131
+
Note: The original array is not modified. If either start or end is negative, it is treated as counting from the end of the array.
0 commit comments