There are some things a for
loop can do that forEach
can’t, and vice versa. for
offers a more fine-grained control flow, allowing to exit early with break
, as well as to iterate conditionally with for ... where ...
. On the other hand, forEach
allows to pass closures to it. In most cases though it simply comes down to personal taste!