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])
Call
What 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
Parameter
Description
list
Required. Array, array-like, or plain object
iteratee
Required. Function (value, index|key, list) or property name
Identity. Default / omitted iteratee keys by the value itself.
Objects. Plain objects tally their values (1, 2, 1).
vs groupBy. Same keys would hold arrays of items, not numbers.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.countByUnderscore 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).