Return sum of all arguments passed to function - Javascript Array

Javascript examples for Array:reduce

Description

Return sum of all arguments passed to function

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.js"></script> 
 </head> //from w w w . j  a va 2s . c o m
 <body> 
  <script type="text/javascript">
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)); 
console.log(sum(1, 2, 3));

      </script>  
 </body>
</html>

Related Tutorials