JavaScript Array reduce() Method17 Mar 2025 | 3 min read The reduce() method reduces the given array into a single value by executing a reducer function. The user implements the reducer function that works on every element present in the array. Reducer FunctionThe reducer function is the user implemented code. It uses the following four arguments to perform its task:
SyntaxParametercallback: The main is the callback function that executes for each element, excluding the first, if no initialValue is specified. The callback function undertakes the following four arguments:
initialValue: It is the first argument value used in the first invocation of the callback function. ReturnIt returns a single value as an output. Points to note:
Thus, it is safe and good to provide the initial value. JavaScript Array reduce() Method ExampleLet's implement some examples to understand better: Example1 Here's a simple example to sum up the array elements and display the output. Test it NowOutput: ![]() Example2 Here's an example to display the difference of the array elements using reduce() method. Test it NowOutput: ![]() Example3 Here's an example to total the array elements using the arrow function. Test it NowOutput: ![]() Example4 When the array contain a single value only. Test it NowOutput: ![]() Thus, in presence of a single array element, there is no need to create a callback function. Also, because when an array contain a single element, the callback is not invoked. We can perform several examples using the array reduce() method. Next TopicJavaScript Array |
JavaScript Array method The JavaScript array method calls the specified function for every array element and returns the new array. This method doesn't change the original array. Syntax The method is represented by the following syntax: array.map(callback(currentvalue,index,arr),thisArg) Parameter callback - It represents the function that produces...
1 min read
JavaScript Array Method The methods perform testing and checks if atleast a single array element passes the test, implemented by the provided function. If the test is passed, it returns true. Else, returns false. Note: If method is applied on an empty array, it...
2 min read
JavaScript Array Method The method is an inbuilt array method that flattens a given array into a newly created one-dimensional array. It concatenates all the elements of the given multidimensional array, and flats upto the specified depth. We can specify the depth limit to...
2 min read
JavaScript Array method The JavaScript array method is used to search the position of a particular element in a given array. This method is case-sensitive. The index position of first element in an array is always start with zero. If an element is not present...
1 min read
JavaScript Array method The JavaScript array method adds one or more elements to the end of the given array. This method changes the length of the original array. Syntax The method is represented by the following syntax: array.push(element1,element2....elementn) Parameter element1,element2....elementn - The elements to be added. Return The original array...
1 min read
JavaScript Array method The JavaScript array method filter and extract the element of an array that satisfying the provided condition. It doesn't change the original array. Syntax The method is represented by the following syntax: array.filter(callback(currentvalue,index,arr),thisArg) Parameter callback - It represents the function that test the condition. currentvalue -...
1 min read
JavaScript Array method The JavaScript array method is used to invoke the specified function once for each array element. Syntax The method is represented by the following syntax: array.forEach(callback(currentvalue,index,arr),thisArg) Parameter callback - It represents the function that test the condition. currentvalue - The current element of array. index - It...
1 min read
JavaScript Array method The JavaScript array method removes the last element from the given array and return that element. This method changes the length of the original array. Syntax The method is represented by the following syntax: array. Return The last element of given array. JavaScript Array method...
1 min read
JavaScript Array Method The method reduces the given array elements into a single value by executing a reducer function. The reducer() function is applied against the accumulator and reduces all the elements from right to left. Syntax array.reduceRight(callback(accumulator,currentValue,currentIndex,array),initialValue) Parameter callback: It is the callback function that executes over...
2 min read
JavaScript Array method The JavaScript array method adds one or more elements in the beginning of the given array and returns the updated array. This method changes the length of the original array. Syntax The method is represented by the following syntax: array. unshift(element1,element2,....,elementn) Parameter element1,element2,....,elementn - The...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India