Skip to main content

Function: sum()

sum(iterable: IterableResolvable<NumberResolvable>): number

Consumes the iterable and returns the sum of all the elements.

Parameters

ParameterTypeDescription
iterableIterableResolvable<NumberResolvable>An iterator of numbers to calculate the sum of.

Returns

number

The sum of the numbers in the input iterator.

Example

import { sum } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
console.log(sum(iterable));
// Output: 15

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/sum.ts:24