sum

sum(array<number> array) → real sum
Computes the sum of all numeric elements of given array.

Returns the sum of all the numeric elements of the array array.

Parameters
Return
TypeScript

In JavaScript there is no native sum: you write a.reduce((s, x) => s + x, 0). Two pitfalls. Without an initial value, reduce raises a TypeError on an empty array: keep the 0. And + concatenates as soon as an element is a string — ["a", "b"].reduce((s, x) => s + x, 0) is the string "0ab", not a number.

Python

In Python, sum() raises a TypeError as soon as an element is not a number, where sum in LeekScript converts every element (a non-numeric string becomes its length). Another difference: the type of the result follows that of the elements. sum([1, 2]) returns the integer 3, whereas sum in LeekScript always returns a real. sum([]) is 0, and a second argument gives the starting value — sum([1, 2], 10) is 13.