Iterator.forEach consumes every remaining value unless the source or procedure fails. The procedure receives a local zero-based counter, its normal return value is ignored, and the method returns undefined after exhaustion. This guide separates those facts from false-return early stopping and array-hole behaviour.
This specialist guide belongs to the Websites root. It also connects to The Gold Standard of Education | Learning That Transfers into Life and the How X Works hub. The worked cases are original teaching examples; their numbers are stipulated so that the reasoning can be checked.
eduKateSG · Websites
Find your next learning step
Trace Iterator.forEach value-index calls, ignored normal callback returns, exhaustion, external effects, errors and iterator closing.
ROUTE 1 · CHAPTERS 1–3
Build the first model
Name the source, consuming method and first complete event trace.
ROUTE 2 · CHAPTERS 4–6
Follow the decisive mechanism
Work through pulls, counters, callback results and completion in order.
ROUTE 3 · CHAPTERS 7–9
Change the context
Change one condition and rebuild the trace from that premise.
ROUTE 4 · CHAPTERS 10–12
Handle limits and evidence
Test cleanup, errors, shared state and evidence that separates rival explanations.
ROUTE 5 · CHAPTERS 13–15
Use and explain the result
Turn the mechanism into dependable tests, reviews and learner explanations.
Chapter contents
Build the first model · Chapters 1–3
1. Define complete procedural traversal
Follow the decisive mechanism · Chapters 4–6
Change the context · Chapters 7–9
Handle limits and evidence · Chapters 10–12
Use and explain the result · Chapters 13–15
13. Compare with Array.prototype.forEach
Iterator.forEach consumes values in order, calls the procedure with each value and a zero-based counter, ignores normal callback return values, and returns undefined after exhaustion. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Source A,B,C produces procedure calls (A,0), (B,1), (C,2), then a done step and final undefined. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Return labels from the procedure. Traversal and final undefined remain unchanged. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. The method is for effects; it does not collect callback results. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
The counter starts at zero and advances after each normally completed procedure call. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Values X,Y receive indices zero and one. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Consume one raw source value before forEach; the first remaining value still receives local counter zero. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. The counter is local traversal state, not the sources original absolute position. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
Any normal value returned by the procedure, including false, true or an object, is discarded. It does not stop iteration. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Procedure returns false for B, yet C is still pulled and processed. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Throw at B instead. Abrupt completion stops before C and triggers closing. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Returning false is not a break statement for Iterator.forEach. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
After the source reports done, forEach returns undefined regardless of the procedures normal results. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Procedure builds an external log and returns numbers; the method result is undefined while the external log contains the effects. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Use an empty source. No calls occur and the method still returns undefined. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. A useful effect must be stored outside the return value of forEach. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
forEach is terminal and consuming. It performs source pulls during the call rather than returning a lazy helper. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Call forEach on a logging generator and observe all events before the call returns. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Call map on the same design and observe no source work until the map helper is advanced. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Do not expect to pause forEach between values from ordinary synchronous caller code. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
Procedure calls follow the order in which next produces values. The method does not sort or batch them. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Source yields C,A,B and the effect log records C0,A1,B2. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Change only yield order and rebuild the log. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Completion order inside asynchronous work started by the callback may differ, but synchronous procedure invocation order remains the source order. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
An empty iterator returns undefined without invoking the procedure. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. The first next is done and the effect log remains empty. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Add one value. The procedure runs at zero before the final done step. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Undefined final result cannot distinguish empty traversal from a non-empty successful traversal. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
Repeated equal values are distinct source events and receive successive counters. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Source yields A three times; the procedure logs A0,A1,A2. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Deduplicate upstream with a Set iterator and compare the shorter event stream. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Value equality does not collapse iterator events inside forEach. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
When forEach consumes filter, its counter counts selected helper outputs while the filter predicate counts every source value examined. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Filter evens from 1,2,3,4; filter sees indices zero through three, forEach sees 2 at zero and 4 at one. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Change predicate to accept all values and the counters align in this narrower fixture. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Adjacent counters belong to different stages even when their numbers happen to match. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
With map upstream, forEach receives mapped values and its local indices; mapping happens immediately before each procedure call during consumption. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Map A,B to A0,B1; forEach logs A0 at zero and B1 at one. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Consume one mapped value first, then forEach sees only B1 at local index zero. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. The procedure does not receive original upstream values unless the mapper preserves them. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
If the procedure throws, IfAbruptCloseIterator closes the source and propagates the error. No later values are pulled. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Procedure completes for A, throws for B at index one, generator finally runs and C is unvisited. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Return false for B instead. Traversal continues to C because the completion is normal. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Thrown error and false return are different protocol outcomes. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
CHAPTER 12 OF 15 · Handle limits and evidence
12. Separate source failure from procedure failure
A next failure occurs before a procedure call for that step, so the procedure log shows only successfully obtained values. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Source yields A then next throws; the procedure log contains A at zero only. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Yield B and throw inside its procedure to observe B at index one before cleanup. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Do not attribute all forEach failures to the procedure. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
CHAPTER 13 OF 15 · Use and explain the result
13. Compare with Array.prototype.forEach
Iterator.forEach consumes produced values. Array.prototype.forEach visits present indexed properties and skips holes according to array rules. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. An array iterator produces undefined for a hole, so iterator forEach can call the procedure where array forEach would skip the absent property. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Fill the hole with explicit undefined and compare the array-method trace. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Shared method names do not erase iterator-versus-indexed-property traversal differences. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
CHAPTER 14 OF 15 · Use and explain the result
14. Design a decisive effects fixture
A strong fixture includes repeated values, a false return that must not stop, a later side effect, a throw case, an empty case and cleanup logging. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Assert B returns false yet C runs; then replace false with throw and assert C is unvisited and return or finally runs. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Consume one raw value before calling forEach to test local counter reset. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. An ordinary all-success array cannot expose ignored return values or abrupt closing. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
A complete account states starting iterator position, value-index procedure calls, ignored normal returns, exhaustion or throw, final undefined or error and cleanup. Begin with the exact receiver, the underlying iterator and the operation that advances it. Similar array methods can help with vocabulary, but the decisive event order must come from the iterator method being studied.
Worked case. Report A at zero returned false normally, B at one ran, done occurred and forEach returned undefined. Write a ledger with one row for each source pull, callback or procedure call when present, Boolean conversion, stored or returned value, completion and return. The ledger makes the stopping point visible instead of hiding it inside one final result.
Trace the case from left to right. Validate the receiver and callable arguments, obtain the direct iterator, request the next value, perform the method-specific step, and interpret the result only after the stopping rule is known. Include the final done step when exhaustion matters.
Changed-context check. Changed report: A throws, the source closes and B is never pulled. Rebuild the trace from the changed premise rather than editing the first answer by intuition. Compare the first event that differs, the values already consumed and the state left in the source; those facts show which parts of the explanation transfer.
Limit and misconception check. Do not describe callback returns as collected outputs or as a normal early-stop signal. State the narrow claim supported by the fixture. A finite generator does not establish behaviour for an infinite source, a throwing callback, a shared iterator or a source whose next and return methods have visible side effects.
For a practical evidence check, log next, return and the callback when one exists. Assert the ordered log as well as the final result. A snapshot of the Boolean, found value, array or undefined result can hide extra pulls, early closing and counter changes.
A strong review separates three questions: what did the source produce, what did the method do with that event, and what did the caller observe? Keeping those layers separate prevents an output position or final value from being mistaken for the source protocol.
Transfer the chapter into a short Websites explanation with four parts: the starting state, the decisive protocol step, the changed-context result and the remaining caveat. This structure lets a learner reuse the reasoning without memorising one fixture as a universal rule.
Continue with connected lessons
Trace Iterator.map callback indices
Trace Iterator.map callback indices. Compare a lazy transformer that yields callback results with forEachs ignored returns.
Separate Iterator.reduce initial-value branches
Separate Iterator.reduce initial-value branches. Compare another consuming callback method whose return values update an accumulator.
Trace Iterator.filter indices
Trace Iterator.filter indices. Keep source counters distinct from selected-output counters in composed pipelines.
Frequently asked questions
What does Iterator.forEach return?
Undefined after normal exhaustion.
Does returning false stop it?
No. Any normal callback return value is ignored.
Where does the callback index start?
At zero for the first value consumed by that forEach call.
Is it a lazy helper?
No. It consumes during the call.
What happens when the procedure throws?
The iterator closes, the error propagates and later values remain unvisited.
How does it differ from Array.prototype.forEach?
Iterator.forEach consumes produced values; array forEach follows present indexed properties and can skip holes.
Authoritative sources and further reading
ECMAScript: Iterator.prototype.forEach. Normative procedure calls, counter, ignored results and exhaustion.
ECMAScript: IteratorClose. Cleanup after abrupt procedure completion.
ECMAScript: Iterator Records. Direct iterator state and next-method use.
Return to the Websites subject guide, The Gold Standard of Education, or the How X Works hub.