// Array of numbers
const numbers = [1, 2, 3, 4, 5];
// Calculate the sum of array elements using the reduce method
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
// Display the sum
console.log('Sum of Array Elements:', sum);
code
JavaScript Sum of Array Elements
This JavaScript code calculates the sum of elements in an array using the reduce method. The reduce method iterates over the array, adding each element to an accumulator (acc). The result is then displayed in the console. The comments provide explanations for each part of the code.
Array
Sum
reduce Method