MediumGenerators & Iterators💻 Output Question

Custom iterable with Symbol.iterator

💡

Hint

Any object with [Symbol.iterator]() is iterable. It must return an object with a next() method.

What does this output?

const range = {
  from: 1, to: 3,
  [Symbol.iterator]() {
    let cur = this.from;
    const last = this.to;
    return {
      next() {
        return cur <= last
          ? { value: cur++, done: false }
          : { value: undefined, done: true };
      }
    };
  }
};
console.log([...range].join(','));

Correct Output

1,2,3

Why this output?

Explanation: [Symbol.iterator]() returns a fresh iterator. Spread calls it and collects until done.

Key Insight: Any object with [Symbol.iterator]() is iterable. It must return an object with a next() method.

More Generators & Iterators Output Questions

MediumGenerator pauses at yield, done true at endMediumreturn in generator — spread ignores return valueMediumyield* delegates to another iterableEasyGenerator as lazy range

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz