Javascript reduce Method with examples 2022

javascript reduce

Hey friends, Welcome to CodeWithNepal today in this blog you’ll see how the javascript array reduce method with a simple example. In the earlier blog, I shared call a function in Javascript which I recommend you to watch.

Javascript Reduce

If you’ve ever worked with arrays in JavaScript, you’ve probably come across the .reduce() method. The .reduce() method is used to reduce an array to a single value.

The .reduce() method takes two arguments: a callback function and an optional initial value. The callback function is called for each element in the array, and the return value of the callback function is used as the final result.

If you provide an initial value, it will be used as the first argument to the callback function. Otherwise, the first element in the array will be used as the initial value.

JavaScript Array reduce() Method

If you’re looking to reduce an array in JavaScript, the .reduce() method is the way to go. This method applies a function to each element in an array, resulting in a single output value.

The reduce() method takes two arguments:

  • The function to apply to each element in the array
  • The initial value of the reduction

If you’re not familiar with the reduce method on arrays, it can be a little confusing at first. Essentially, the reduce method allows you to iterate over an array and perform some operation on each element in the array. The operation can be anything you want, but it typically involves taking two elements and combining them in some way. The reduce method then returns a single value, which is the result of the operation.

So, why would you want to use the reduce method?

Well, there are a few reasons. First, it can be a more efficient way to perform an operation on an array than using a for a loop. Second, it can be used to perform operations that are not possible with a for loop, such as finding the average of all the elements in an array.

To see how the reduce method works, let’s take a look at a simple example. We have an array of numbers and we want to find the sum of all the numbers.

Below is the example of the Array reduce() method:

Here’s a simple example of using the .reduce() method to sum up all the values in an array:

const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;

}, 0);

console.log(sum); // 15

In the example above, we start with an array of numbers and use .reduce() to find the sum of those numbers. The function we pass to .reduce() takes two arguments: the accumulator and the current value. The accumulator is the running total of our calculation, and the current value is 15.

You may like this blog:

Array reduce() Method example

<!DOCTYPE html>
		JavaScript Array reduce() Method

<body style="text-align:center;">
	<h1 style="color: blue;">CodeWithNepal</h1>
		Click here to get the Subtract
		of array elements from the left side
	<button onclick="mycode()">
		Click Here!
	Subtract: <span id="GFG"></span>
	<!-- Script to use reduce method -->
		var arr = [175, 50, 25];

		function subofArray(total, num) {
			return total - num;
		function mycode(item) {
					= arr.reduce(subofArray);



Please enter your comment!
Please enter your name here