What are Generators and Iterators in JavaScript?
Suggested answer
Iterator protocol: An object is iterable if it has a [Symbol.iterator]() method returning an iterator with next(). next() returns {value, done}. Built-in iterables: arrays, strings, Maps, Sets. Used by for...of, spread, destructuring. Generator functions (function*): Return a Generator object (which is both an iterable and an iterator). yield pauses execution and returns a value; next() resumes. Example: function* range(start, end) { for (let i = start; i <= end; i++) yield i; }. Generators produce values lazily — useful for large sequences without creating arrays. yield* delegates to another iterable. Generators can also receive values via next(value). Async generators (async function*) work with for await...of for async data streams.
Practice content for interview preparation; not an official vendor answer. Verify details against current product documentation.
Community comments (0)
No comments yet.
Sign in or create a free account to add a comment. Comments are moderated before they appear.