Reduce Method in JavaScript

Ans- The reduce() method in JavaScript is used to apply a function to each element in an array, resulting in a single output value. It iterates through the array and accumulates a single value by executing a provided function on each element.

Basic Syntax-

JavaScript
array.reduce(callbackFunction, initialValue)

Parameters:

  • callbackfuntion: It’s a fucntion that gets executed on each element of the array. It takes four arguments:
    • Accumulator- The result of the pervious iteration.
    • Current value- the current element being processed.
    • Current index- optional: the index of the current element being processed.
    • Source array- Optional, the array reduce() was called upon.
  • initiaValue: An optional parameter representing the initial value or the initial value for the accumulator.

Example-

JavaScript
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 15

Example-

JavaScript
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
 return accumulator + currentValue;
}, 0);
console.log(sum); // Output: 15

Example-

JavaScript
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr);
console.log(sum); // 15

Note:

  • accumulator starts at 0.
  • currentValue: takes each element of the array in sequence.
  • reduce() method can be used for a variety of operations beyond summation, such as finding the maximum/Minium value, transforming data structure, or filtering data, the key is to define the appropriate logic within the callback function to achieve the desired result.

0%
1 votes, 5 avg
39

Start Your Coding Journey!!

Thanks for Participating!!!


Created on By PC Prajapat

JavaScript Interview Questions Quiz Part 3

Register 

1 / 10

Which of the following operators has the lowest precedence?

2 / 10

What will the following code output?

let x = 5;
let y = Object.freeze({x});
y.x = 10;
console.log(y.x);

3 / 10

What is the output of the following code?

(function(){
var a = b = 3;
})();
console.log(typeof b);

4 / 10

What is the result of null == undefined in JavaScript?

5 / 10

Which of the following correctly clones an object in JavaScript?

6 / 10

How does the ‘this’ keyword behave inside an arrow function in JavaScript?

7 / 10

How does the ‘this’ keyword behave inside an arrow function in JavaScript?

8 / 10

Which of the following methods will stop further event propagation in JavaScript?

9 / 10

What will be the output of the following code?

function test() {
var a = b = 5;
}
test();
console.log(typeof b);

10 / 10

Which of the following is not a primitive data type in JavaScript?

Your score is

The average score is 41%

Share with your friends.

LinkedIn Facebook
0%

Exit

Rate us.

Thanks for your valuable feedback.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *