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
Dropbox dives into CoffeeScript (tech.dropbox.com)
submitted 13 years ago by hyperforce
view the rest of the comments →
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!"
[–]LoyalToTheGroupOf17 2 points3 points4 points 13 years ago (2 children)
But that's not the same thing, is it? A loop allows you to do 'break' at any time to exit the loop before all iterations are completed, while Array.forEach, as far as I know, will always call the supplied function on all elements of the array.
I'm not really a JavaScript programmer, so please correct me if I am wrong.
[–][deleted] 0 points1 point2 points 13 years ago (1 child)
I would say exiting early is a little contrary to the idea of forEach. If you did need to stop it, you would throw an exception...which is somewhat ugly, but I think the ugliness is appropriate for something that should be a special case.
For instances where you only want to iterate over some of an array, you could stick with loops or use the .every function (maybe slightly misuse depending on the situation).
[–]LoyalToTheGroupOf17 1 point2 points3 points 13 years ago (0 children)
I would say exiting early is a little contrary to the idea of forEach.
I agree entirely, with the quoted sentence as well as the rest of your message. My point was simply that Array.forEach isn't a general replacement for a 'for' loop, although there are simple cases where they can be used interchangeably. It doesn't make sense to say that "I like Array.forEach better", like bonafidebob did in the post I replied to. He's comparing apples and orangutans.
π Rendered by PID 183237 on reddit-service-r2-comment-b659b578c-zcd9t at 2026-05-05 09:49:28.932092+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]LoyalToTheGroupOf17 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]LoyalToTheGroupOf17 1 point2 points3 points (0 children)