JavaScript Interview Questions Tutorial
JavaScript Interview Questions For Beginners - ES6
Q. What is JavaScript Array reduce() method?
A. In Javascript ES6, reduce method is used to perform some operation on each array element. Below is the syntax:
let numbers = [1, 2, 3];
let sum = numbers.reduce(function (accumulator, currentValue) {
return accumulator + currentValue;
});
console.log(sum);
Output:
6
Here, we are adding all elements in the array and saving its result in sum variable. Each variable details:
- numbers: is a array variable
- sum: is the result of adding each element in array
- accumulator: indicates the previous element value in the loop
- currentValue: is the value of current element