Get the Sum of an Array of Numbers in JavaScript-Code Examples

Radu TM

Get the Sum of an Array of Numbers in JavaScript

JavaScript by Radu TM June 21, 2022

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

// initialize a variable to hold the sum
let sum = 0;

// iterate over the array
for (const number of numbers) {
  // add the current number to the sum
  sum += number;
}

// print the sum to the console
console.log(sum);

0

18.976

Get the Sum of an Array of Numbers in JavaScript

JavaScript by Radu TM June 21, 2022

// Get the sum of an array of numbers in JavaScript
const numbers = [1, 2, 3, 4, 5];

let sum = 0;

for (const number of numbers) {
  sum += number;
}

console.log(sum); // 15

0

18.976

Get the Sum of an Array of Numbers in JavaScript

JavaScript by Radu TM June 21, 2022

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

let sum = 0;

for (const number of numbers) {
  sum += number;
}

console.log(sum); // => 15

0

18.976

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories