Get to know MDN better
Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The take() method of Iterator instances returns a new iterator helper object that yields the given number of elements in this iterator and then terminates.
The number of elements to take from the start of the iteration.
A new iterator helper object. The returned iterator helper yields the elements in the original iterator one-by-one, and then completes (the next() method produces { value: undefined, done: true }) once limit elements have been yielded, or when the original iterator is exhausted, whichever comes first.
Thrown if limit becomes NaN or negative when converted to an integer.
The following example creates an iterator that yields terms in the Fibonacci sequence, and then logs the first three terms:
take() is most convenient when you are not hand-rolling the iterator. Because iterators are also iterable, you can iterate the returned helper with a for...of loop:
Because fibonacci() is an infinite iterator, using a for loop to iterate it without any logic to exit early (such as a break statement) would result in an infinite loop.
You can combine take() with Iterator.prototype.drop() to get a slice of an iterator:
When the limit is negative or NaN, a RangeError is thrown:
When the limit is larger than the total number of elements the iterator can produce (such as Infinity), the returned iterator helper has essentially the same behavior as the original iterator:
| ECMAScript® 2027 Language Specification # sec-iterator.prototype.take |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.