Underscore.js _.chunk() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Chunks an array into multiple arrays, each containing length or fewer items (official Underscore docs). Stable Array helper — not deprecated.

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

Syntax

Call _.chunk. Start with this line.

Underscore
_.chunk(array, length)
CallWhat it does
_.chunk(array, length)Primary signature.
_.chunk(...)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
_.chunk(array, length)

Parameters

ParameterDescription
arrayRequired. Source to split
lengthRequired. Max items per chunk (must be ≥ 1)

Return Value

Return ValueDescription
*New array of chunk arrays

Underscore.js Examples

Example 1 — Even Split

When the count divides evenly, every chunk has the same length.

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

How It Works

  1. Source. Six numbers; indexes 0 … 5.
  2. First group. Length 2 takes 1 and 2.
  3. Next groups. Same step for 3,4 then 5,6.
  4. Even fit. No remainder — three chunks of length 2.

Example 2 — Official: Shuffle Then Pair

The docs pair a shuffled list into groups of two — great for random partners.

Underscore
var kindergarten = [
  "Tyrone", "Elie", "Aidan", "Sam",
  "Katrina", "Billie", "Little Timmy"
];

var partners = _.chunk(_.shuffle(kindergarten), 2);
// → e.g. [["Tyrone","Elie"], ["Aidan","Sam"],
//         ["Katrina","Billie"], ["Little Timmy"]]
// (pair order varies after shuffle)
Try It Yourself

How It Works

  1. Shuffle first. _.shuffle randomizes the list so pairs are not always the same.
  2. Chunk by 2. Walk left to right and cut pairs.
  3. Odd count. Seven names → last chunk has one leftover.
  4. Shape. Always an array of arrays; inner order depends on the shuffle.

Example 3 — Shorter Last Chunk

Leftover elements stay in the final group — nothing is padded.

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

How It Works

  1. First chunk. Take three items: 1,2,3.
  2. Second chunk. Next three: 4,5,6.
  3. Remainder. One item left → alone in the last chunk.
  4. Outer length. ceil(7 / 3) = 3.

Example 4 — Empty Array & Non-Positive Length

Empty input or invalid length both yield an empty outer array.

Underscore
_.chunk([], 3);          // → []
_.chunk([1, 2, 3], 0);   // → []
_.chunk([1, 2, 3], -1);  // → []
_.chunk([1, 2, 3], 10);  // → [[1, 2, 3]]
Try It Yourself

How It Works

  1. Empty source. Nothing to group → [] (not [[]]).
  2. Bad length. 0 or negative is invalid → Underscore returns [].
  3. Oversized length. All items fit in one chunk.
  4. Rule. Always pass a positive length you control.

Browser & Runtime Support

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

_.chunk()

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

_.chunk(array, length) splits an array into multiple arrays, each containing length or fewer items. Example: _.chunk([1,2,3,4,5], 2) returns [[1,2],[3,4],[5]].
No. It returns a new outer array of chunk arrays. The source array is left unchanged. Object elements inside are still shared by reference.
The final chunk holds the leftover items and can be smaller than length. Nothing is padded or dropped.
Underscore returns [] when length is null, undefined, zero, or negative. Unlike Lodash, there is no default size of 1 — always pass a positive length.
You get a single chunk containing the whole array.
_.chunk splits by count into sequential groups. _.partition splits by a predicate into two groups: [pass, fail]. Use chunk for batches; partition for true/false filters.

Next: _.indexOf()

Find the first index of a value in an array — with an optional sorted binary search.

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