Design & Download
Your JS Library
in Seconds!!!

Snippet: sum()

Written by Christopher West (cwest) on January 12, 2016.
Finds the sum of an array of numbers.
function sum(x) {
  for (var result = 0, nums = isArrayLike(x) ? x : arguments, i = nums.length; i--;) {
    result += +nums[i];
  }
  return result;
}

sum(...) API Documentation

Finds the sum of an array of numbers.

Parameters

  1. x {Array<number>|number}:
    If this is an array of numbers, the numbers within the array will be summed up and returned. Otherwise, this will be the first number to which all subsequent numbers will be added.
  2. ...otherNumbers {number=}:
    If x is not an array, all subsequent parameters will be added to x to produce the sum of all of the numbers.

Returns

If x is an array of numbers, the sum of the array of numbers will be returned. Otherwise the sum of the numbers passed in as parameters will be returned.