iterator.windows(n) returns a lazy iterator of arrays. Each array is a sliding window of n neighbouring values. Windows overlap: the next window drops the first item and adds the next source value.
By default the mode is "only-full", so a leftover shorter than n is skipped. Pass "allow-partial" as the second argument if you want that short final window when the source is shorter than n (and not empty). For non-overlapping batches, use chunks().
Remember
iterator.windows(n)
iterator.windows(n, "only-full") // default
iterator.windows(n, "allow-partial")
overlapping sliding windows
next window reuses neighbours
only-full → skip short leftover
allow-partial → keep short window
need separate batches? → chunks(n)
Syntax
Call windows(n) for full sliding windows, or pass a second string for short-source behaviour.
Required. Length of each sliding window. Must be a positive integer.
underSized
Optional. "only-full" (default) ignores a short leftover; "allow-partial" yields it when the source has some values but fewer than windowSize. Other strings throw TypeError.
Return Value
Return Value
Description
Iterator helper
A new lazy iterator. Each step yields one array that overlaps the previous window. An empty source yields nothing.
JavaScript Examples
Example 1 — Walk Sliding Windows of 2
Turn four numbers into overlapping pairs with windows(2).
JavaScript
const pairs = [1, 2, 3, 4].values().windows(2).toArray();
for (const pair of pairs) {
console.log(pair.join(" → "));
}
The Try It page first checks that iterator.windows() 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
First window. The helper pulls 1 and 2 together.
Slide forward. Drop 1, keep 2, add 3, then later 3 and 4.
Print pairs. Each line shows one overlapping pair in order.
Example 2 — Compare only-full and allow-partial
When the source is shorter than the window size, the second argument decides whether anything is yielded.
The Try It page first checks that iterator.windows() 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
Too short for 3. Two values cannot fill a full window of size 3.
Default skips."only-full" yields nothing, so the array length is 0.
Partial keeps them."allow-partial" yields the short array [1, 2].
Example 3 — Compare Each Neighbour Pair
Use windows of 2 to see whether each next number is bigger than the previous one.
JavaScript
const steps = [3, 5, 4, 7].values().windows(2);
for (const [a, b] of steps) {
console.log(a + " → " + b + ": " + (b > a ? "up" : "not up"));
}
The Try It page first checks that iterator.windows() 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
Pair neighbours.windows(2) yields [3, 5], [5, 4], and [4, 7].
Destructure.for (const [a, b] of steps) reads both ends of each window.
Compare. Print whether b is greater than a for each step.
Compatibility
Browser & Runtime Support
Iterator.prototype.windows() 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.windows()
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.windows()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 yields overlapping arrays of length n. Each next window drops the first value and adds the next value from the source iterator.
windows() overlaps so neighbours are reused. chunks() makes separate non-overlapping batches, and its last chunk may be shorter.
If the whole source is shorter than n (but not empty), "allow-partial" yields that short array once. The default "only-full" yields nothing in that case.
Treat each yielded array as a snapshot for that step. Pull values into your own storage if you need to keep older windows around while iterating.
It is a newer helper with limited support. Feature-detect with typeof Iterator.prototype.windows === "function" before you use it.