Underscore.js _.countBy() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Sorts a list into groups and returns a count for the number of objects in each group — similar to groupBy, but returns counts instead of lists of values (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 _.countBy. Start with this line.

Underscore
_.countBy(list, iteratee, [context])
CallWhat it does
_.countBy(list, iteratee, [context])Primary signature.
_.countBy(...)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
_.countBy(list, iteratee, [context])

Parameters

ParameterDescription
listRequired. Array, array-like, or plain object
iterateeRequired. Function (value, index|key, list) or property name
contextOptional. Bound as this inside the iteratee

Return Value

Return ValueDescription
*New object: key → number

Underscore.js Examples

Example 1 — Official Even / Odd Tally

Count how many numbers are even vs odd.

Underscore
_.countBy([1, 2, 3, 4, 5], function (num) {
  return num % 2 == 0 ? "even" : "odd";
});
// → { odd: 3, even: 2 }
Try It Yourself

How It Works

  1. Iteratee. Returns "even" or "odd" for each number.
  2. Increment. Underscore adds 1 under that key.
  3. Totals. Three odds, two evens → { odd: 3, even: 2 }.
  4. Docs sample. This is the official Underscore example.

Example 2 — Property Name Shorthand

Pass a string to tally by that field — no custom function.

Underscore
var users = [
  { name: "Ada", role: "admin" },
  { name: "Lin", role: "user" },
  { name: "Grace", role: "user" }
];

_.countBy(users, "role");
// → { admin: 1, user: 2 }
Try It Yourself

How It Works

  1. Shorthand. "role" means “use each item’s role as the key.”
  2. Ada. admin → count becomes 1.
  3. Lin & Grace. Both user → count becomes 2.
  4. Use case. Fast role / status / category tallies for dashboards.

Example 3 — Count Words by Length

Property shorthand works on strings too — tally by length.

Underscore
_.countBy(["one", "two", "three", "four"], "length");
// → { "3": 2, "4": 1, "5": 1 }
Try It Yourself

How It Works

  1. Keys. Each word’s length becomes an object key ("3", "4", "5").
  2. one / two. Both length 3 → 2.
  3. three / four. Lengths 5 and 4 → one each.
  4. Note. Integer-like keys often stringify in numeric order.

Example 4 — Empty List & Identity

No items → empty map; omit a custom iteratee to count equal values.

Underscore
_.countBy([], _.identity);
// → {}

_.countBy(["a", "b", "a", "a"]);
// → { a: 3, b: 1 }

_.countBy({ x: 1, y: 2, z: 1 }, function (n) {
  return n % 2 ? "odd" : "even";
});
// → { odd: 2, even: 1 }
Try It Yourself

How It Works

  1. Empty. Nothing to tally → {}.
  2. Identity. Default / omitted iteratee keys by the value itself.
  3. Objects. Plain objects tally their values (1, 2, 1).
  4. vs groupBy. Same keys would hold arrays of items, not numbers.

Browser & Runtime Support

_.countBy 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

_.countBy()

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
_.countBy 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

_.countBy(list, iteratee, [context]) groups items by a key and returns how many land in each group. Official example: even/odd tallies for [1, 2, 3, 4, 5] → {odd: 3, even: 2}.
A key for that item — usually a string or number. Underscore uses that key on the result object and increments its count.
Yes. _.countBy(users, "role") is shorthand for reading each item’s role property.
Same key logic. groupBy stores arrays of items; countBy stores only the number per key. Use countBy when you need totals, not the items.
No. It returns a new plain object. The source collection is unchanged.
Yes. For a plain object it tallies the object’s values; the iteratee receives (value, key, list).

Next: _.shuffle()

Return a randomized copy of a list with Underscore’s Fisher–Yates shuffle.

_.shuffle() 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