JavaScript Iterator windows() Method

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

Definition

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.

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

const pairs = it.windows(2).toArray();

console.log(pairs);   // [[1, 2], [2, 3], [3, 4]]
CallWhat it does
iterator.windows(n)Sliding windows of length n (default "only-full").
iterator.windows(n, "allow-partial")Also yield a shorter window when the source is shorter than n.
iterator.windows(n, "only-full")Skip any leftover shorter than n (the default).
iterator.windows(2).toArray()Collect every window into one outer array.

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(), chunks(), drop(), every(), filter(), find(), flatMap(), forEach(), includes(), join(), map(), reduce(), some(), take(), toArray(), [Symbol.dispose](), [Symbol.iterator]().

Library declarations
JavaScript
// Newer Iterator helper (limited support)
Iterator.prototype.windows(windowSize)
Iterator.prototype.windows(windowSize, underSized)

// windowSize : positive integer (1 .. 2^32 - 1)
// underSized : "only-full" (default) | "allow-partial"
// returns    : a lazy iterator of arrays

Parameters

ParameterDescription
windowSizeRequired. Length of each sliding window. Must be a positive integer.
underSizedOptional. "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 ValueDescription
Iterator helperA 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(" → "));
}
Try It Yourself

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

  1. First window. The helper pulls 1 and 2 together.
  2. Slide forward. Drop 1, keep 2, add 3, then later 3 and 4.
  3. 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.

JavaScript
const fullOnly = [1, 2].values().windows(3).toArray();
const partial = [1, 2].values().windows(3, "allow-partial").toArray();

console.log("only-full count: " + fullOnly.length);
console.log("allow-partial: " + (partial[0] ? partial[0].join(", ") : "(none)"));
Try It Yourself

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

  1. Too short for 3. Two values cannot fill a full window of size 3.
  2. Default skips. "only-full" yields nothing, so the array length is 0.
  3. 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"));
}
Try It Yourself

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

  1. Pair neighbours. windows(2) yields [3, 5], [5, 4], and [4, 7].
  2. Destructure. for (const [a, b] of steps) reads both ends of each window.
  3. Compare. Print whether b is greater than a for each step.

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.

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

Next: [Symbol.dispose]()

Learn how Iterator [Symbol.dispose]() closes an iterator by calling return() for cleanup.

[Symbol.dispose]() 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