Skip to content

sum

Calculates the sum of an array of numbers. null and undefined are treated as empty arrays and return 0.

Available since v2.0.0

import { sum } from '@helpers4/array';
sum(array: readonly number[] | null | undefined): number
ParameterTypeDescription
arrayreadonly number[] | null | undefinedThe array of numbers to sum

number — The sum of all values, or 0 for an empty array, null, or undefined

Calculates the sum of an array of numbers.

sum([1, 2, 3, 4])
// => 10

Handles negative numbers correctly.

sum([10, -3, 5, -2])
// => 10

View source on GitHub