Underscore.js _.size() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Return the number of values in the list (official Underscore docs). Stable Collection helper — not deprecated.

Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.size. Start with this line.

Underscore
_.size(list)
CallWhat it does
_.size(list)Primary signature.
_.size(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.size(list)

Parameters

ParameterDescription
listRequired. Array, object, string, or array-like

Return Value

Return ValueDescription
*Number of values

Underscore.js Examples

Example 1 — Official: Array Length

Count elements in an array.

Underscore
_.size([1, 2, 3, 4, 5]);
// → 5
Try It Yourself

How It Works

  1. Input is an array. Arrays expose .length.
  2. _.size reads that length. Five numbers → 5.
  3. Same as [1, 2, 3, 4, 5].length for arrays.
  4. Why use it? Same call works later if the value becomes an object.

Example 2 — Official: Object Keys

Count own properties on a plain object.

Underscore
_.size({one: 1, two: 2, three: 3});
// → 3
Try It Yourself

How It Works

  1. Plain objects have no useful .length.
  2. _.size counts keys one, two, three.
  3. Equals _.keys(obj).length for that object.
  4. Inherited props from the prototype are not counted.

Example 3 — String vs .length

Strings are measured like arrays — character count.

Underscore
_.size("hello");
// → 5

var word = "hello";
console.log(word.length);  // → 5  (same for strings)
Try It Yourself

How It Works

  1. Strings are array-like for length purposes.
  2. _.size("hello") returns 5 characters.
  3. Native .length matches for strings and arrays.
  4. Use _.size when the type might also be a plain object.

Example 4 — Empty & Nullish

Safe defaults when data might be missing.

Underscore
_.size([]);
// → 0

_.size({});
// → 0

_.size(null);
// → 0

_.size(undefined);
// → 0
Try It Yourself

How It Works

  1. Empty collection → count is zero.
  2. null / undefined → Underscore returns 0 (no throw).
  3. Guards UI paths where data may not have loaded yet.
  4. Need a boolean? Prefer _.isEmpty(list).

Browser & Runtime Support

_.size ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.size()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.size Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.size(list) returns the number of values in the list. Official examples: _.size([1, 2, 3, 4, 5]) → 5 and _.size({one: 1, two: 2, three: 3}) → 3.
For arrays and strings, _.size is the same as .length. For plain objects (no .length), _.size counts own enumerable keys — one helper for every collection type.
It counts own enumerable keys — the same set _.keys returns. Inherited properties are not included.
_.size(null) and _.size(undefined) return 0. Empty arrays, objects, and strings also return 0.
Yes. Array-like values are measured by their length, so you get the element count directly.
No. It is a stable Underscore Collection helper.

Next: _.partition()

Split a list into two arrays — items that pass a predicate, and those that fail.

_.partition() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful