DEV Community

Discussion on: Old School Loops in JavaScript - for loop and forEach

Collapse
 
genuinerex profile image
Rex Bloom

Breaking and returning from a for loop is tempting, but error prone. Using Functional techniques is a much better solution and leads to cleaner more testable code. Use .find to retrieve the first matching element. Closures are not a serious overhead concern that we should avoid them to conserve memory.

I agree with Michael, while loops should be avoided.

I personally cannot remember the last time I used while and for.

But to each their own. You have to maintain and read your code so write it however you want!