How to sum the numbers in an array with JavaScript

2 Answers

0 votes
const arr = [6, 8, 1, 2, 5, 9];
  
const sum = arr.reduce((x, y) => x + y);

console.log(sum); 
  
  
  
    
      
      
/*
run:
      
31
      
*/

 



answered Jun 4, 2021 by avibootz
edited Jun 29, 2022 by avibootz
0 votes
const arr = [6, 8, 1, 2, 5, 9];
 
let sum = 0;

for (let num of arr) {
    sum = sum + num
}
 
console.log(sum); 
 
 
 
   
     
     
/*
run:
     
31
     
*/

 



answered Jun 4, 2021 by avibootz
...