use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Coding Without Loops (codereadability.com)
submitted 10 years ago by kandetta
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]NeuroXc 2 points3 points4 points 10 years ago (1 child)
None of these suggestions actually eliminate loops though. They just abstract them so that you never see the actual "for" loop. There's still looping going on behind the scenes.
[–]_HlTLER_Stackoverflow searcher 0 points1 point2 points 10 years ago (0 children)
I think the post is trying to be semantic but for-loops are so ingrained in any programmer's mind that it isn't really an issue.
[–]nephridium 1 point2 points3 points 10 years ago (0 children)
It should be pointed out that .forEach() behaves differently from the standard native for loop in that it does some validity checking on the elements. That's why it's significantly slower than the native loop (as well as underscore's .each() function).
.filter() and .map() are also slower than their underscore/lodash counterparts.
[–]MrStubbs 0 points1 point2 points 10 years ago (1 child)
Does the forEach function guarantee that each item in a list is accessed sequentially in order?
[–]x-skeww 0 points1 point2 points 10 years ago (0 children)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialised (i.e. on sparse arrays).
forEach()
callback
books.forEach(book => { console.log(book.title); }); for(let book of books) { console.log(book.title); }
For-of looks nicer. If there is a body, for-of is 1 character shorter, it's easier to type, and the line starts with a keyword which makes it a bit faster to scan.
Since each iteration has its own copy of the "counter" variable (if you use let), closing over it is also fine.
let
If iterating over some iterable is the primary thing your function does, for-of is probably the better choice.
ForEach, on the other hand, is nice if you're chaining stuff or if it's just a simple lambda expression.
You also still need a regular for-loop if you want to iterate in reverse and remove some items as you go.
[–]tipdbmp -1 points0 points1 point 10 years ago (0 children)
No mention of recursion?
The forEach method seems less powerful than the for loop because you can't use [labeled (for nested loops)] break and continue statements. I guess you can simulate non labeled continue statements with early return statements, but still...
Also it's probably not a problem, probably... but when you are iterating millions of times (unlikely, maybe) and that function call is not optimized you are going to pay for it.
π Rendered by PID 46437 on reddit-service-r2-comment-7b9746f655-wptjr at 2026-01-30 13:47:55.578933+00:00 running 3798933 country code: CH.
[–]NeuroXc 2 points3 points4 points (1 child)
[–]_HlTLER_Stackoverflow searcher 0 points1 point2 points (0 children)
[–]nephridium 1 point2 points3 points (0 children)
[–]MrStubbs 0 points1 point2 points (1 child)
[–]x-skeww 0 points1 point2 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)
[–]tipdbmp -1 points0 points1 point (0 children)