2

One way is to use arguments. I can loop over the arguments array and can return the sum of all the arguments passed.

function sum(){
  var sum =0; 
  for(var i=0;i<arguments.length;i++){
     sum += arguments[i];
  }
   return sum;
}
sum(1,2); // returns 3
sum(1,2,3); // returns 6

Is there any other way to do it without using loop?

Alexander Ciesielski
  • 10,506
  • 5
  • 45
  • 66
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

8 Answers8

12

Other people provided answers with redundant copying of arguments to an array that is to be thrown away in a moment.

Instead you can do everything in one step:

function sum() {
    return Array.prototype.reduce.call(arguments, function(a, b) {
        return a + b;
    }, 0);
}

If using ES2015 is an option you can have slightly nicer (subjective) implementation:

const sum = (...args) => [...args].reduce((a, b) => a + b, 0);
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    Do you need to use the Spread operator ([...args].reduce) to apply the reduce method? It worked for me without it, i.e. args.reduce. From the Mozilla docs: "The arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly." – Rob Gravelle Aug 18 '22 at 15:53
  • @RobGravelle that's a really nice catch. I also see now that t_dom93 does exactly that in their answer below too. I suspect those days ES2015 still was felt "new" (assuming nobody or almost nobody supported it natively?) so that ton of new language features was tricky to learn all at once :-) PS: `...` is _not_ an operator :-P – zerkms Aug 19 '22 at 02:02
1

As zerkms said, You can use reduce function like this

alert([1, 2].reduce((a, b) => a + b, 0));
alert([1, 2, 3].reduce((a, b) => a + b, 0));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Arun AK
  • 4,353
  • 2
  • 23
  • 46
1

Use Array.prototype.slice.call to convert arguments into array, and use reduce to sum up numbers.

 function sum(){
   var total =  Array.prototype.slice.call(arguments).reduce(function(a, b) { 
   return a + b; 
   }, 0);
   return total;
}
console.log(sum(1,2)); // returns 3
console.log(sum(1,2,3)); // returns 6

Demo: https://jsfiddle.net/wru8mvxt/10/

Joey Etamity
  • 856
  • 4
  • 9
1

One-liner with rest parameters:

const sum = (...args) => args.reduce((a, b) => a + b);

sum(1, 3, 5, 7, 9); // 25
t_dom93
  • 10,226
  • 1
  • 52
  • 38
0

Arguments is an array like object in javascript. You can use reduce property to convert to sum it is elements.

function sum() {
  var args = Array.prototype.slice.call(arguments);
  var sum = args.reduce(function(a, b) {
    return a + b;
  })
  return sum;
}


console.log(sum(1, 2)); // returns 3
console.log(sum(1, 2, 3)); // returns 6

You can also use forEach method

JSFIDDLE

brk
  • 48,835
  • 10
  • 56
  • 78
0

In simple way you you can achieve it by using following

function sum(){
  var total =  0;
  for(var i=0; i<arguments.length; i++){
     total += arguments[i];
  }
  return total;
}
console.log(sum(1, 2)); // returns 3
console.log(sum(1, 2, 3)); // returns 6

Or

Also by using Array.prototype.reduce.call() method.

function sum(){
   var total =  Array.prototype.reduce.call(arguments, function(a, b) { 
   return a + b; 
   });
   return total;
}      
console.log(sum(1, 2));    // returns 3
console.log(sum(1, 2, 3));   // returns 6
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
0

Here is my solution with ES15:

function sumArgs() {
    return [...arguments].reduce((acc,next)=>acc+next, 0)
};

console.log(sumArgs(1,2,3,4,5));
JulienRioux
  • 2,853
  • 2
  • 24
  • 37
0

It's a bit of a late answer I guess but this easy method and logic will benefit others I'm sure.

var val = 0;
function sum(){
    for(i of arguments){
        val += i;
    }console.log(val)
}

sum(10,20,40,30,110) //Output: 210
Alesko
  • 39
  • 2
  • 5