What is the purpose of the reduce() method in JavaScript arrays?
a) To reduce the length of an array
b) To apply a function to each element and accumulate the result
c) To remove duplicates from an array
d) To remove elements from an array
Answer:
b) To apply a function to each element and accumulate the result
Explanation:
The reduce()
method executes a reducer function on each element of the array, resulting in a single output value. It is often used to sum or concatenate array elements, but it can also perform more complex transformations.