⚖️for-of vs. forEach
JS⟩ syntax ⟩ for loops ⟩ for-of ⟩ vs. forEach
Don't use forEach
. (In fact, never use forEach
anywhere in ES6).
forEach
has known pitfalls that make it unsuitable in some situations that can be properly handled with for
or for-of
:
callback function creates new context (can be addressed with arrow function)
doesn't support iterators
doesn't support generator
yield
andasync..await
doesn't provide a proper way to terminate a loop early with
break
Last updated