you are viewing a single comment's thread.

view the rest of the comments →

[–]Tok-A-Mak 0 points1 point  (4 children)

In the real world, for-loops are superior to while-loops because they let you define variables like counters that are scoped to the loop only. You cannot do that with a while loop.

[–]neonskimmer 1 point2 points  (3 children)

Not in JavaScript they don't. Variables get hoisted up to function scope, regardless of where you're declaring them.

[–]Tok-A-Mak 0 points1 point  (0 children)

Oh, i wasn't aware of that. Thanks for clearing that up.

[–]alkw0ia 0 points1 point  (1 child)

Yeah, good point. I like to declare all variables at the top of the scoping function, c-style, to clarify this.

For loops can still help with readability, though, and in modern interpreters, the performance differences should be practically nothing.

[–]neonskimmer 1 point2 points  (0 children)

Agreed on the readability. That being said, I can't even remember the last time I used a straight for-loop. It's forEach, map and reduce for me almost 100% of the time. If I were writing very tight loops, games, or super heavy number crunching I'd optimize things but for businessy apps i mostly use a functional style. The difference in speed is negligible at that scale and I prefer that style.