for-of vs. forEach
Last updated
Was this helpful?
Last updated
Was this helpful?
⟩ syntax ⟩ ⟩ ⟩ vs. forEach
always use in ES6.
It works on any
It supports all kinds of control flow in the loop body, like continue
, break
, return
, yield
and await
.
👉
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
and async..await
doesn't provide a proper way to terminate a loop early with break
👉