JavaScript Iterator

Beginner
8 min read
Updated: Nov 2026
3 examples
Lazy sequences

Definition and Usage

An Iterator is an object that produces values one at a time. You ask for the next value with next(), and you get an object like { value, done }. When done is true, there are no more values.

An iterable (such as an array, a Map or a string) can create an iterator when you need one, usually through [Symbol.iterator](). Loops like for...of use that protocol for you.

This page explains the idea. The Iterator Methods index links the full guides for every helper, from Iterator.from() to toArray().

Iterator helpers need a current browser.

Methods such as map(), filter(), take() and toArray() are Baseline Newly available (2025). They work in current browsers and in Node.js 22 or newer. Always feature-detect in older environments.

JavaScript Syntax

Get an iterator, pull values, and use a helper chain:

JavaScript
const it = [10, 20, 30].values();

console.log(it.next());   // { value: 10, done: false }
console.log(it.next());   // { value: 20, done: false }

const doubled = [1, 2, 3, 4].values()
  .filter((n) => n % 2 === 0)
  .map((n) => n * 2)
  .toArray();

console.log(doubled);     // [4, 8]

Core Ideas

Ideas every Iterator tutorial in this series builds on:

IdeaExampleRemember
Iteratorit.next()An object that yields { value, done } one step at a time
Iterablefor (const v of arr)Anything with [Symbol.iterator]() that can make an iterator
Lazy helpersit.map(fn)Transform values only when they are pulled
Iterator.from()Iterator.from(x)Make a proper Iterator from an iterable or plain next() object
take() / drop()it.take(3)Limit or skip values without building a full array first
toArray()it.toArray()Collect remaining values into a new Array
Infinite sourcesfunction* fib()Generators can go forever; helpers like find() stop early

Iterator helpers vs Array methods

Many helpers look like array methods. The big difference is when the work happens:

FeatureArray methodsIterator helpers
When work runsUsually right away on the whole arrayLazily, when you ask for the next value
MemoryOften builds new arrays at each stepCan avoid big temporary arrays
Infinite dataNot practicalWorks with take(), find(), and similar
Starting pointAn ArrayAny iterator (arrays, maps, generators, ...)
Finish the chainAlready an arrayOften call toArray() or loop with for...of

Use array methods when you already have a small array and want a simple style. Use iterator helpers when you want lazy pipelines, or when the source is a generator or another iterator.

How an Iterator Works

Pull, do not push

You ask for the next value. The iterator does not dump everything at once. That is why large or infinite sequences are possible.

done means finished

When next() returns { done: true }, stop. The value is usually undefined at that point.

Helpers share the source

A helper such as map() reads from the same underlying iterator. Do not expect to replay the same iterator twice unless you create a new one.

Chain, then collect

A common pattern is .filter(...).map(...).take(n).toArray(). Keep toArray() at the end so earlier steps stay lazy.

Closing early

Some iterators have a return() method. [Symbol.dispose]() calls it so resources can be cleaned up when you stop early.

Real-Life Uses

Iterators show up everywhere in modern JavaScript. Common uses:

UseHow an Iterator helps
Process without a huge arrayFilter and map a stream of values from a Map, a Set or a generator without copying everything first.
Stop earlyUse find(), take() or some() on a long or infinite sequence and stop as soon as you have an answer.
Readable pipelinesChain drop(), filter() and map() so each step has one clear job.
Pair related dataUse Iterator.zip() or zipKeyed() when you want matching positions from several iterables.

JavaScript Examples

Three short programs. Each one prints plain text that is the same every time you run it. Iterator helpers need a current browser or Node.js 22+.

Example 1 — Call next() by Hand

Create an array iterator and pull values with next() until done is true.

JavaScript
const it = ["a", "b"].values();

console.log(JSON.stringify(it.next()));
console.log(JSON.stringify(it.next()));
console.log(JSON.stringify(it.next()));

How It Works

  1. Get an iterator. values() returns an Array Iterator for the values "a" and "b".
  2. Pull twice. The first two next() calls give the letters and done: false.
  3. Finished. The third call reports done: true. There is nothing left to read.

Example 2 — Filter, Map and Take

Build a short lazy pipeline and collect the result with toArray().

JavaScript
const result = [1, 2, 3, 4, 5, 6].values()
  .filter((n) => n % 2 === 0)
  .map((n) => n * 10)
  .take(2)
  .toArray();

console.log(result.join(", "));
map() tutorial

How It Works

  1. Keep even numbers. filter() only lets 2, 4 and 6 through, still as an iterator.
  2. Multiply. map() turns them into 20, 40 and 60, still lazily.
  3. Take two and collect. take(2) stops after 20 and 40. toArray() builds the final array.

Example 3 — Upgrade a Plain Iterator

Wrap a simple next() object with Iterator.from() so helper methods become available.

JavaScript
let n = 0;
const plain = {
  next() {
    n += 1;
    if (n > 3) return { done: true };
    return { value: n, done: false };
  }
};

const it = Iterator.from(plain);
console.log(it.toArray().join(", "));

How It Works

  1. Plain next only. The object has next() but no helper methods of its own.
  2. Upgrade. Iterator.from(plain) returns a proper Iterator that inherits the helpers.
  3. Collect. toArray() pulls 1, 2 and 3 into an array and prints them.

Iterator Methods

Iterator has static methods, many instance helpers, and two special symbol methods. Each one has its own tutorial:

MethodWhat it does
Iterator.concat()Yield several iterables in sequence.
Iterator.from()Make a proper Iterator from an iterable or next() object.
Iterator.zip()Pair values from several iterables into arrays.
Iterator.zipKeyed()Pair values into objects with named keys.
map()Transform each value lazily.
filter()Keep values that pass a test.
take()Take only the first N values.
toArray()Collect remaining values into an Array.

Full list: Iterator Methods index.

Frequently Asked Questions

An Iterator is an object with a next() method that returns { value, done }. Each call gives the next value. When done is true, the sequence is finished.
An iterable can create an iterator (usually with [Symbol.iterator]()). An iterator is the object you pull values from with next(). Arrays are iterable; array.values() returns an iterator.
Helpers are lazy: they do work only when you pull values. That saves memory and lets you stop early with take() or find(), even on infinite generators.
Call array.values() (or keys()/entries()), then chain helpers: [1,2,3].values().map(n => n * 2).toArray(). You can also use Iterator.from(array).
The core helpers (map, filter, take, drop, toArray and friends) are Baseline Newly available since 2025 in current Chrome, Firefox, Safari and Node.js 22+. Newer methods such as concat, zip and chunks need even newer engines — always feature-detect.

Ready for the method guides?

Open the Iterator Methods index to learn every method with syntax, runnable examples and FAQs.

Iterator Methods index →

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.

16 people found this page helpful