JavaScript Iterator chunks() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

iterator.chunks(n) returns a new lazy iterator that groups the source values into arrays of length n. The groups do not overlap: the next chunk starts where the previous one ended.

If the source runs out mid-chunk, the last array can be shorter than n. An empty source yields nothing (no empty array). For overlapping groups, use windows() instead.

Remember
iterator.chunks(n)

non-overlapping groups of length n
last chunk may be shorter
empty source → no chunks

returns a lazy iterator of arrays
finish with toArray() or for...of

need overlap? → use windows(n)

Syntax

Call chunks(n) on an iterator. Each yielded value is an array of up to n items.

JavaScript
const it = [1, 2, 3, 4, 5].values();

const groups = it.chunks(2).toArray();

console.log(groups);   // [[1, 2], [3, 4], [5]]
CallWhat it does
iterator.chunks(n)Yield non-overlapping arrays of length n (last may be shorter).
iterator.chunks(n).toArray()Collect every chunk into one outer array.
[1, 2, 3, 4].values().chunks(2)Start from an array iterator with .values().
iterator.take(10).chunks(5)Limit first, then split into batches.

A newer Iterator helper on Iterator.prototype. Support is limited today, so feature-detect before you rely on it. No import required. Siblings: Iterator.concat(), Iterator.from(), Iterator.zip(), Iterator.zipKeyed(), drop(), every(), filter(), find(), flatMap(), forEach(), includes(), join(), map(), reduce(), some(), take(), toArray(), windows(), [Symbol.dispose](), [Symbol.iterator]().

Library declarations
JavaScript
// Newer Iterator helper (limited support)
Iterator.prototype.chunks(chunkSize)

// chunkSize : positive integer (1 .. 2^32 - 1)
// returns   : a lazy iterator of arrays

Parameters

ParameterDescription
chunkSizeRequired. How many values go in each chunk. Must be a positive integer. Non-integers throw TypeError; values below 1 throw RangeError.

Return Value

Return ValueDescription
Iterator helperA new lazy iterator. Each step yields one array of up to chunkSize values from the source. The source is not copied into a big array up front.

JavaScript Examples

Example 1 — Split Values into Chunks of 2

Take numbers 1 through 4 and group them into pairs with chunks(2).

JavaScript
const groups = [1, 2, 3, 4].values().chunks(2).toArray();

for (const group of groups) {
  console.log(group.join(", "));
}
Try It Yourself

The Try It page first checks that iterator.chunks() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Start an iterator. [1, 2, 3, 4].values() yields the four numbers in order.
  2. Chunk by 2. The first pull takes 1 and 2; the next takes 3 and 4.
  3. Print each pair. toArray() collects both chunks, then the loop prints them.

Example 2 — See a Shorter Last Chunk

When the count is not a multiple of n, the final chunk keeps the leftover values.

JavaScript
const groups = [1, 2, 3, 4, 5].values().chunks(2).toArray();

console.log("Count: " + groups.length);
console.log("Last: " + groups[groups.length - 1].join(", "));
console.log("Last length: " + groups[groups.length - 1].length);
Try It Yourself

The Try It page first checks that iterator.chunks() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Five values, size 2. Full pairs are [1, 2] and [3, 4].
  2. One value left. 5 still becomes its own chunk instead of being dropped.
  3. Check the end. There are 3 chunks, and the last one has length 1.

Example 3 — Limit Then Batch

Take only the first six values from a longer list, then split them into batches of three.

JavaScript
const batches = [10, 20, 30, 40, 50, 60, 70, 80].values()
  .take(6)
  .chunks(3)
  .toArray();

console.log(batches.map((b) => b.join("-")).join(" | "));
Try It Yourself

The Try It page first checks that iterator.chunks() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Take six. take(6) stops after 10 through 60.
  2. Chunk by 3. Those six values become two arrays of three.
  3. Print batches. Joining each batch shows 10-20-30 and 40-50-60.

Browser & Runtime Support

Iterator.prototype.chunks() is a newer Iterator helper for grouping values. Support is limited today (Firefox and recent Safari; Chromium is still catching up). Always feature-detect.

✓ Newer · Limited support

iterator.chunks()

Supported in Firefox 154+ and Safari 27.3+. Chromium, Opera and Node.js support is still catching up. Always feature-detect.

Limited Standard since
Google Chrome Not yet · check updates
Not supported
Microsoft Edge Not yet · check updates
Not supported
Mozilla Firefox 154+ · Desktop & Mobile
Full support
Apple Safari 27.3+ · macOS & iOS
Partial / recent
Opera Not yet · check updates
Not supported
Samsung Internet Not yet · check updates
Not supported
Android WebView Not yet · check updates
Not supported
Node.js Not yet · check updates
Not supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
iterator.chunks() Limited

Bottom line: Feature-detect before use. Until Chromium ships it, build chunks or sliding windows with a short loop over the iterator.

Frequently Asked Questions

It splits the source iterator into non-overlapping arrays of length n. Each next() on the helper pulls up to n values and yields them together as one array.
Yes. If fewer than n values remain, they are still yielded as a shorter array. An empty source yields no chunks at all.
chunks() makes separate batches with no shared values. windows() makes sliding windows that overlap, so neighbours are reused.
No. It returns a lazy iterator of arrays. Call toArray() or loop with for...of when you want the chunks collected.
It is a newer helper with limited support. Feature-detect with typeof Iterator.prototype.chunks === "function" before you use it.

Next: drop()

Learn how to skip the first n values from an iterator, then keep reading the rest.

drop() 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.

11 people found this page helpful