sum

Sum the numbers in an array

Definition

number sum(array[number] $array)

Sums all the elements of a number array. An error is raised if there is an element that is neither a number nor a boolean.

Parameters

array[number] $array

An array to sum the elements of.

Returns

number

The sum of all the elements of $array.

Examples

{
  a: sum(`[1, 2, 3]`),
  b: sum(`[-1, -2, 3]`),
  c: sum(`[1.0, -2.5, 3.1]`),
  d: sum(`[true, false, 0]`),
  e: sum(`[]`),
  f: sum(['string', `1`, `0`])       // Error
}
{
  "a": 6,
  "b": 0,
  "c": 1.6,
  "d": 1,
  "e": 0
}