Wednesday, November 19, 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
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6858 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS