Lodash _.chunk() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • What _.chunk(array, size) returns and when to use it.
  • How the last chunk behaves when the length is not a multiple of size.
  • How to import chunk without pulling in all of Lodash.
  • Open each numbered example in the Try it editor (?tryit=1, 2, 3) with Lodash loaded from a CDN.
  • Practical pitfalls around size and empty arrays.

Prerequisites

Comfort with JavaScript arrays and a project where you can run small snippets (browser console or Node).

  • You know what an array literal is and that indexes start at 0.
  • You can npm install lodash or already have Lodash available in your app.

Overview

_.chunk splits an array into consecutive groups of size elements. The result is an array of smaller arrays. If elements do not divide evenly, the final group holds the remainder.

Non-destructive

The source array is not modified; you receive a new outer array of chunks.

Batching

Useful for UI pagination slices, queue workers, and CSV row batches.

Predictable length

Outer length is Math.ceil(array.length / size) for positive size.

Syntax

javascript
_.chunk(array, [size=1])
  • array: the collection to split (array-like values are coerced).
  • size: positive integer length for each chunk except possibly the last.
  • Returns: a new array containing chunk arrays.
1

Even split

When the length is a multiple of size, every inner array has the same length.

javascript
import chunk from "lodash/chunk";

const rows = chunk(["a", "b", "c", "d"], 2);
// [["a", "b"], ["c", "d"]]
Try it Yourself
2

Last chunk shorter

Remainder elements stay together in the final chunk.

javascript
import chunk from "lodash/chunk";

const rows = chunk([1, 2, 3, 4, 5], 2);
// [[1, 2], [3, 4], [5]]
Try it Yourself
3

Empty input

An empty source array produces an empty outer array.

javascript
import chunk from "lodash/chunk";

chunk([], 3);
// []
Try it Yourself

📋 _.chunk vs manual loops

ApproachProsCons
_.chunkShort, tested, consistent across environmentsAdds a dependency (or one more import)
for loop with sliceNo dependencyMore boilerplate and easier to mishandle bounds

Pitfalls to avoid

Size

Zero or negative size

Lodash coerces size with toInteger and clamps; non-positive sizes are unsafe to rely on. Pick a positive chunk size you control.

Objects

Shared references

Chunking does not clone objects inside the array; every chunk still points at the same object instances.

Huge batches

Memory

Materializing all chunks at once duplicates structure in memory; for very large streams consider iterators or streaming APIs instead.

❓ FAQ

No. It returns a new array containing chunk arrays. The original array is left unchanged.
The last inner array holds the remaining elements and can be shorter than size.
When size is omitted or undefined (and not guarded by iteratee detection), Lodash treats it as 1 so each chunk is a single element.
Use import chunk from "lodash/chunk" or require("lodash/chunk") so tree-shaking can drop unused Lodash modules.
It is fine for in-memory batches. For SQL pagination you still need LIMIT and OFFSET (or cursor) at the query layer; chunk only reshapes arrays you already hold.

Summary

  • Purpose: _.chunk(array, size) groups elements into fixed-length batches.
  • Immutability: the original array is unchanged; you get a new nested array.
  • Next: Lodash _.compact() or return to the array methods hub.
Did you know?

_.chunk always returns a new outer array; inner chunks reference the same element values as the source array (objects are not cloned).

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.

6 people found this page helpful