JavaScript “For” Loop Techniques You Might Not Know

Jason Knight
CodeX
Published in
10 min readJan 3, 2022

--

Some of the “for loops” I’ve used in recent demo’s and rewrites for people have raised eyebrows. Constructs and methodologies that are very powerful, but even a lot of advanced programmers either don’t know, have never even seen, or just plain won’t use for some pedantic fairy tale reason.

Right now we have a lot of know-nothings running around screaming about “teh evuls” of “for loops” just like the nutjobs pissing, moaning, and kvetching about how “if/else” and “switch/case” are “bad”. I maintain that those making these claims know nothing about programming, and certainly don’t understand the raw efficiency of for loops in general.

There are a lot of techniques that can be leveraged to make them many times more effective than things like Array.foreach, Array.map, and so forth. Much less those cases where you just know you need to do “x” number of things. Some of the bizzaro-land hoops I’ve seen folks jump through just to avoid using a “for loop” is getting outright absurd.

Because nothing says “easier’ or “better” like writing two to ten times the code needed and spreading it out over 20 separate files, to avoid one to three lines of direct code.

The following techniques can result in cleaner, faster, and simpler code.

Let’s Talk About The Parts Of a For Loop

This is pretty basic stuff most developers THINK they know, but there are implications that even many advanced…

--

--

Jason Knight
CodeX

Accessibility and Efficiency Consultant, Web Developer, Musician, and just general pain in the arse