or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# Lodash Sum

1

2

Lodash Sum provides the `_.sum` method exported as a standalone Node.js module. It computes the sum of all values in an array with robust handling of edge cases like empty arrays and undefined values.

3

4

## Package Information

5

6

- **Package Name**: lodash.sum

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash.sum`

10

11

## Core Imports

12

13

```javascript

14

const sum = require('lodash.sum');

15

```

16

17

For ES modules environments (CommonJS package):

18

19

```javascript

20

import sum from 'lodash.sum';

21

```

22

23

## Basic Usage

24

25

```javascript

26

const sum = require('lodash.sum');

27

28

// Basic array summation

29

const result = sum([4, 2, 8, 6]);

30

console.log(result); // => 20

31

32

// Handle empty arrays

33

const empty = sum([]);

34

console.log(empty); // => 0

35

36

// Handle null/undefined input

37

const nullResult = sum(null);

38

console.log(nullResult); // => 0

39

```

40

41

## Capabilities

42

43

### Array Summation

44

45

Computes the sum of all values in an array, handling edge cases gracefully.

46

47

```javascript { .api }

48

/**

49

* Computes the sum of the values in `array`.

50

*

51

* @static

52

* @memberOf _

53

* @since 3.4.0

54

* @category Math

55

* @param {Array} array The array to iterate over.

56

* @returns {number} Returns the sum.

57

* @example

58

*

59

* _.sum([4, 2, 8, 6]);

60

* // => 20

61

*/

62

function sum(array);

63

```

64

65

**Behavior Details:**

66

67

- **Empty arrays**: Returns `0`

68

- **Falsy input**: Any falsy value (null, undefined, false, 0, "", etc.) returns `0`

69

- **Undefined array elements**: Skipped during summation (not included in result)

70

- **Non-numeric values**: Processed through JavaScript's automatic type coercion during addition

71

- **Type**: Pure function with no side effects

72

73

**Usage Examples:**

74

75

```javascript

76

const sum = require('lodash.sum');

77

78

// Numeric arrays

79

sum([1, 2, 3, 4, 5]); // => 15

80

sum([10.5, 20.25, 5.75]); // => 36.5

81

82

// Arrays with mixed types (numeric coercion applies)

83

sum([1, '2', 3]); // => 6

84

sum(['10', '20', '30']); // => 60

85

86

// Edge cases

87

sum([]); // => 0

88

sum([undefined, 1, 2]); // => 3 (undefined elements are skipped)

89

sum([1, , 3]); // => 4 (sparse array holes become undefined, are skipped)

90

sum(null); // => 0

91

sum(undefined); // => 0

92

sum(false); // => 0

93

sum(0); // => 0

94

95

// Single value arrays

96

sum([42]); // => 42

97

sum([0]); // => 0

98

sum([-5, 5]); // => 0

99

```

100

101

## Error Handling

102

103

The `sum` function is designed to be robust and does not throw errors under normal usage:

104

105

- Any falsy input (null, undefined, false, 0, "", NaN) returns `0`

106

- Arrays containing undefined elements skip those elements during summation

107

- Non-numeric array elements undergo JavaScript's automatic type coercion during addition

108

- No input validation or type checking is performed - the function relies on JavaScript's truthy/falsy evaluation