Course contentsShow
← JavaScript
Lesson 1101 of 2,72325. Iterators and GeneratorsFree lesson

The Value and Done Properties

Examining the object returned by next(), containing value (the yielded value) and done (completion status).

The Value and Done Properties

You'll learn exactly what the iterator result object contains and how to interpret its two key properties.

When you call next() on an iterator (such as one returned by a generator function), you don't get the yielded value directly. Instead, you receive an iterator result object with two properties:

The Result Object Structure

Every time you call next(), you get back an object that looks like this:

{
  value: /* the yielded value */,
  done: /* true or false */
}

The value Property

This holds whatever was yielded by the generator (or returned at the end). It's the actual data the iterator is producing at this step.

function* counter() {
  yield 1;
  yield 2;
}

const gen = counter();
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }

The done Property

This boolean tells you whether the iterator has finished. When done is false, there are more values coming. When done is true, the iteration is complete.

function* oneValue() {
  yield 'hello';
}

const gen = oneValue();
console.log(gen.next()); // { value: 'hello', done: false }
console.log(gen.next()); // { value: undefined, done: true }

Notice that after the last yield, calling next() again gives you undefined for value and true for done.

Think of It Like a Vending Machine

The result object is like a vending machine's response: value is the item that drops down, and done tells you if the machine is empty.

Key Takeaway: The next() method always returns an object with value (the current data) and done (whether iteration has finished), allowing you to control and track the iteration process explicitly.