Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Iterator
  6. take()

Iterator.prototype.take()

Baseline 2025
Newly available

Since ⁨March 2025⁩, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Thetake() method ofIterator instances returns a newiterator helper object that yields the given number of elements in this iterator and then terminates.

Syntax

js
take(limit)

Parameters

limit

The number of elements to take from the start of the iteration.

Return value

A newiterator helper object. The returned iterator helper yields the elements in the original iterator one-by-one, and then completes (thenext() method produces{ value: undefined, done: true }) oncelimit elements have been yielded, or when the original iterator is exhausted, whichever comes first.

Exceptions

RangeError

Thrown iflimit becomesNaN or negative whenconverted to an integer.

Examples

Using take()

The following example creates an iterator that yields terms in the Fibonacci sequence, and then logs the first three terms:

js
function* fibonacci() {  let current = 1;  let next = 1;  while (true) {    yield current;    [current, next] = [next, current + next];  }}const seq = fibonacci().take(3);console.log(seq.next().value); // 1console.log(seq.next().value); // 1console.log(seq.next().value); // 2console.log(seq.next().value); // undefined

Using take() with a for...of loop

take() is most convenient when you are not hand-rolling the iterator. Because iterators are also iterable, you can iterate the returned helper with afor...of loop:

js
for (const n of fibonacci().take(5)) {  console.log(n);}// Logs:// 1// 1// 2// 3// 5

Becausefibonacci() is an infinite iterator, using afor loop to iterate it without any logic to exit early (such as abreak statement) would result in an infinite loop.

Combining drop() with take()

You can combinetake() withIterator.prototype.drop() to get a slice of an iterator:

js
for (const n of fibonacci().drop(2).take(5)) {  // Drops the first two elements, then takes the next five  console.log(n);}// Logs:// 2// 3// 5// 8// 13for (const n of fibonacci().take(5).drop(2)) {  // Takes the first five elements, then drops the first two  console.log(n);}// Logs:// 2// 3// 5

Lower and upper bounds of take count

When thelimit is negative orNaN, aRangeError is thrown:

js
fibonacci().take(-1); // RangeError: -1 must be positivefibonacci().take(undefined); // RangeError: undefined must be positive

When thelimit is larger than the total number of elements the iterator can produce (such asInfinity), the returned iterator helper has essentially the same behavior as the original iterator:

js
for (const n of new Set([1, 2, 3]).values().take(Infinity)) {  console.log(n);}// Logs:// 1// 2// 3

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-iterator.prototype.take

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp