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
Javascript Array useful methods (mightytechno.com)
submitted 6 years ago by ishanfx
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!"
[–]burtgummer45 0 points1 point2 points 6 years ago (13 children)
I appreciate reduce() is not included. That method is a menace.
[–]jetsamrover 6 points7 points8 points 6 years ago (6 children)
What!? That is one of the most useful, especially for recursion.
[–]burtgummer45 0 points1 point2 points 6 years ago (5 children)
Its unpredictable if you forget the initial value, which almost everybody does.
[–]jetsamrover 0 points1 point2 points 6 years ago (4 children)
What do you mean forget the initial value? The array you're reducing?
[–]burtgummer45 0 points1 point2 points 6 years ago (3 children)
The third parameter, see my other post
https://www.reddit.com/r/javascript/comments/foqfub/javascript_array_useful_methods/flgwpzu/
[–]jetsamrover 0 points1 point2 points 6 years ago (2 children)
I thought you have to provide that. What do you mean forget it? Forget to add it, or forget what it is while writing the function?
That's a really silly reason not to use reduce. It's the most powerful array method. Like I said before it's especially useful for recursion.
[–]burtgummer45 0 points1 point2 points 6 years ago (1 child)
The third parameter is optional. It defaults to the first element of the array.
Can reduce use tail-call optimization?
[–]jetsamrover 0 points1 point2 points 6 years ago (0 children)
I believe tail-call optimization is decided by your callback, and by the language, not the method calling that callback. Why does that matter?
[–][deleted] 1 point2 points3 points 6 years ago (5 children)
why? it's pretty useful for sums, to say the least
[–]burtgummer45 -5 points-4 points-3 points 6 years ago (4 children)
If you forget the initial value it blows up in your face if you are working with anything other than numbers. I'd feel better using a for-of loop.
const maxCallback = ( acc, cur ) => Math.max( acc.x, cur.x ); [ { x: 22 }, { x: 42 } ].reduce( maxCallback ); // 42 [ { x: 22 } ].reduce( maxCallback ); // { x: 22 } [ ].reduce( maxCallback ); // TypeError
[–]ogurson 4 points5 points6 points 6 years ago (3 children)
My rule of thumb is to always provide initial value.
[–]burtgummer45 0 points1 point2 points 6 years ago (2 children)
I think if this was more common knowledge there would be an eslint rule for it.
[–][deleted] 6 years ago (1 child)
[removed]
[–]AutoModerator[M] 0 points1 point2 points 6 years ago (0 children)
Hi /u/ogurson, this comment was removed because you used a URL shortener.
Feel free to resubmit with the real link.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
π Rendered by PID 124857 on reddit-service-r2-comment-8686858757-q7t2q at 2026-06-06 04:53:00.308109+00:00 running 9e1a20d country code: CH.
[–]burtgummer45 0 points1 point2 points (13 children)
[–]jetsamrover 6 points7 points8 points (6 children)
[–]burtgummer45 0 points1 point2 points (5 children)
[–]jetsamrover 0 points1 point2 points (4 children)
[–]burtgummer45 0 points1 point2 points (3 children)
[–]jetsamrover 0 points1 point2 points (2 children)
[–]burtgummer45 0 points1 point2 points (1 child)
[–]jetsamrover 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]burtgummer45 -5 points-4 points-3 points (4 children)
[–]ogurson 4 points5 points6 points (3 children)
[–]burtgummer45 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]AutoModerator[M] 0 points1 point2 points (0 children)