Skip to content
Related Articles
Open in App
Not now

Related Articles

Lodash _.sum() Method

Improve Article
Save Article
Like Article
  • Last Updated : 09 Sep, 2020
Improve Article
Save Article
Like Article

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.sum() method is used to find the sum of the values in the array. 

Syntax:

_.sum(array)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • array: This parameter holds the array to iterate over.

Return Value: This method returns the sum of the values in the array. 

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
    
// Use of _.sum () method 
let gfg = _.sum([5, 10, 15, 20, 25]); 
        
// Printing the output  
console.log(gfg);


Output:

75

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
    
// Use of _.sum () method 
let gfg = _.sum([8, -12, -13, 30, 45]); 
        
// Printing the output  
console.log(gfg);


Output:

58
My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!