Array, String, Map, Set, TypedArray, arguments, and NodeList are iterable by default. for...of calls the [Symbol.iterator]() method and loops over the value property of each next() result until done is true.
Under the hood, for...of obtains the iterator by calling the object's [Symbol.iterator]() method, then repeatedly calls the iterator's next() method, storing the value property in the loop variable and stopping when done is true. Example: const arr = [1,2,3]; for (const v of arr) { console.log(v); }
You need to loop over the keys of a plain object and also over an array of numbers. Which built‑in structures can you use directly with a for...of loop, and what would happen if you tried it on a plain object?
Write a short snippet that uses for...of to iterate over a Map and logs each key/value pair. What does the loop receive on each iteration?
If you have a Set containing duplicate values, how does for...of iterate over it compared to a regular array?
Your team added a feature that processes user‑submitted data stored in a TypedArray. The code uses for...of, but performance seems slower than a classic for loop. Explain why this might be and how you would decide which loop to keep.
During a bug hunt you notice that a for...of loop over an arguments object yields nothing. Why does this happen, and how would you fix the iteration without converting to an array?
We need to refactor a utility that currently uses for...in on an array to a for...of. What edge cases should you watch for, and how does the iterator protocol affect the change?
Design a library that accepts any iterable (Array, Map, Set, String, TypedArray, etc.) and applies a transformation function lazily. How would you leverage the built‑in iterator protocol and for...of internally, and what performance considerations arise at scale?
Our codebase has a custom collection class that currently implements Symbol.iterator incorrectly, causing for...of to skip elements. How would you diagnose the issue and ensure compatibility with all built‑in iterables?
When streaming large data from a server, we want to process chunks using for...of over a ReadableStream. Explain how the async iterator works under the hood and what trade‑offs exist compared to event callbacks.
We are migrating a legacy monolith that heavily uses for...in on array‑like objects to a modern codebase that prefers for...of and async iterables. What architectural guidelines would you set to avoid subtle bugs across teams, and how would you phase the migration?
A cross‑team initiative aims to standardize data handling by exposing all collections as iterables, including database cursors and in‑memory caches. What design patterns and contract decisions would you propose to ensure consistent behavior of for...of across heterogeneous sources?
Consider a high‑throughput microservice that processes millions of events per second using for...of over a custom circular buffer. How would you evaluate the impact of the iterator protocol on GC pressure and latency, and what alternatives might you recommend?