Saturday, October 25, 2025
HomeLanguagesJavascriptLodash _.size() Method

Lodash _.size() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.
The _.size() method gets the size of collection by returning its length for array such as values or the number of own enumerable string keyed properties for objects.

Syntax:

_.size(collection)

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

  • collection: This parameter holds the collection to inspect.

Return Value: This method returns the collection size.

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

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.size() method
var gfg = _.size([12, 14, 36, 29, 10]);
  
// Printing the output 
console.log(gfg);


Output:

5

Example 2:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.size() method
var gfg = _.size({ 'p': 1, 'q': 2, 'r': 5});
  
// Printing the output 
console.log(gfg);


Output:

3

Example 3:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.size() method
var gfg1 = _.size('neveropen');
var gfg2 = _.size('computer science');
  
// Printing the output 
console.log(gfg1, gfg2);


Output:

13, 16

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS