← BYTES
JavaScript·Intermediate·Ref: 3.2.8

What is the practical difference between `Array.prototype.forEach` and a `for...of` loop regarding `break`/`continue`?

Have a question about this video? Mention reference 3.2.8 when you contact us.

a.They behave identically in every respect
b.`forEach`'s callback cannot be stopped early with `break`/`continue` (since it's just a function call per element); a `for...of` loop supports both natively
c.forEach always runs faster because it is asynchronous
d.for...of cannot iterate over arrays, only Maps and Sets
Because forEach invokes a plain callback function once per element, there is no loop construct for `break`/`continue` to act on -- returning early from the callback just skips to the next element, it can't stop the whole iteration. A `for...of` loop is a real loop statement and supports both control-flow keywords directly.

Find this on YouTube & Instagram

Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:

#BYTES#BYTES328#JavaScript