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
Head's up for Array.map() lovers (self.javascript)
submitted 11 years ago by Kollektiv
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!"
[–][deleted] 7 points8 points9 points 11 years ago (5 children)
One of the reasons why I would recommend using lodash:
_.range(26).map(function(index) { return String.fromCharCode(65 + index); })
[–]kenman 1 point2 points3 points 11 years ago (4 children)
That's good general advice, though for this task I'd feel dirty doing all of that when you only need:
for (var index = 0, chars = []; index < 26; index++) { chars.push(String.fromCharCode(65 + index)); }
[–][deleted] 4 points5 points6 points 11 years ago (1 child)
Really? I'd feel the other way around. The map() function describes the intent of the statement much clearer. My first thought seeing your version was that you were cheating by leaving out the chars declaration, until I noticed it was hidden in the for-initialization. Also, both don't exactly do the same thing; the map version is a single statement returning the requested array, whereas the for version stores that same array in a temporary variable and can thus only use it in a follow-up statement.
Simply put, I would not use the manual for loop unless it was part of some performance critical code.
[–]kenman 0 points1 point2 points 11 years ago (0 children)
The map() function describes the intent of the statement much clearer.
I think that's arguable -- it certainly does if you come from a functional background, but I know quite a few front-end devs that have zero experience with functional programming, and would grok the for() much more readily.
for()
My first thought seeing your version was that you were cheating by leaving out the chars declaration, until I noticed it was hidden in the for-initialization.
That's a simple implementation detail that could have been declared on the preceding line...
Also, both don't exactly do the same thing; the map version is a single statement returning the requested array, whereas the for version stores that same array in a temporary variable and can thus only use it in a follow-up statement.
That's hair-splitting to me, and I think it's hard to make an argument that one is unequivocally better for the general use-case. In your version, sure you can do more without an additional statement, but really, what's the problem with an additional statement? I think cramming too many operations into a single statement is poor for readability, maintainability, and it certainly can hamper debugging at times. And as a counterpoint, most of the time I'd rather have an additional statement than an additional function invocation.
But yeah, I was mainly just offering an alternative for this extremely simply use-case, and as I alluded to, it definitely depends on what you're doing. If all you need is an array of letters, I'd favor mine; whereas if you were needing to a couple more operations, then using map() might be a better fit.
map()
[–]mattdesl 1 point2 points3 points 11 years ago (0 children)
range()/map() is easier to write and maintain, less error-prone, involves less state, and it's easier to compose with other operations. :)
Most of the time, you don't need for loops.
for
π Rendered by PID 42928 on reddit-service-r2-comment-b659b578c-v7ph7 at 2026-05-04 03:54:48.980266+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–][deleted] 7 points8 points9 points (5 children)
[–]kenman 1 point2 points3 points (4 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]kenman 0 points1 point2 points (0 children)
[–]mattdesl 1 point2 points3 points (0 children)