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]]
Call
What it does
iterator.chunks(n)
Yield non-overlapping arrays of length n (last may be shorter).
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
Start an iterator.[1, 2, 3, 4].values() yields the four numbers in order.
Chunk by 2. The first pull takes 1 and 2; the next takes 3 and 4.
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.
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
Five values, size 2. Full pairs are [1, 2] and [3, 4].
One value left.5 still becomes its own chunk instead of being dropped.
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.
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
Take six.take(6) stops after 10 through 60.
Chunk by 3. Those six values become two arrays of three.
Print batches. Joining each batch shows 10-20-30 and 40-50-60.
Compatibility
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.
LimitedStandard since
Google ChromeNot yet · check updates
Not supported
Microsoft EdgeNot yet · check updates
Not supported
Mozilla Firefox154+ · Desktop & Mobile
Full support
Apple Safari27.3+ · macOS & iOS
Partial / recent
OperaNot yet · check updates
Not supported
Samsung InternetNot yet · check updates
Not supported
Android WebViewNot yet · check updates
Not supported
Node.jsNot yet · check updates
Not supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerNo 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.