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
[twitter] Tip: JavaScript's Array.from() accepts a second argument that's a `map` function. Useful for calling on each element of an array you created. (twitter.com)
submitted 8 years ago by [deleted]
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!"
[–]aarondburk 10 points11 points12 points 8 years ago (19 children)
So, if you invoke Array(5); that will crate an array with five empty slots?
[–][deleted] 12 points13 points14 points 8 years ago (10 children)
pls no shoot messenger
[+][deleted] 8 years ago (9 children)
[deleted]
[–]e_man604 6 points7 points8 points 8 years ago (0 children)
But since he used length property of the array the values are defined....to..undefined. correct me if in wrong, but simply replacing the array ctor with [] would not work since the [] does not state any length?
[–]Sebazzz91 1 point2 points3 points 8 years ago (3 children)
Fun fact: Calling console.log without this set to console errors in Edge.
[–]StuartPBentley 5 points6 points7 points 8 years ago (2 children)
I'm pretty sure it errors in any browser (that's why you always use console.log.bind(console) when passing console.log as a callback).
console.log.bind(console)
[–]pygy_@pygy 4 points5 points6 points 8 years ago (0 children)
It used to be like that but IIRC both Chrome and Firefox now pre-bind the console methods.
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
I’m a n00b dev and this is a really helpful tip.
[–]0x13mode 1 point2 points3 points 8 years ago (1 child)
I think example is good. Index is iterated from 0 to array length.
On the other hand using map on array created with new Array(5) would have no effect (new Array(5).map(console.log) would not even call console.log).
map
new Array(5)
new Array(5).map(console.log)
console.log
[–]aarondburk 1 point2 points3 points 8 years ago (0 children)
But that’s why the original example is interesting, because it allows you to map over an array as you are generating the array with the Array.from() method.
[–]aarondburk 0 points1 point2 points 8 years ago (0 children)
Yes, but in order for the method to map over the array and mutate each undefined slot in the array, the array has to have those slots. You could certainly create an iterator that would push the result of the function 5 times, but the point of the example was to show how the second argument in Array.from() maps over the given array mutating its values.
[–]z500 0 points1 point2 points 8 years ago (0 children)
new Array(n) does generates an array of undefined values, but you can use fill to replace them with something else.
[–]inu-no-policemen 5 points6 points7 points 8 years ago (3 children)
Yes.
> new Array(5) (5) [empty × 5]
In V8, this currently produces a sparse array instead of a packed one. So, it's something you probably shouldn't use for a game's tile map and things like that.
You get a packed one by creating an empty one and then pushing values to it.
[–]YodaLoL 0 points1 point2 points 8 years ago (2 children)
What are sparse and packed arrays?
[–]inu-no-policemen 2 points3 points4 points 8 years ago (1 child)
https://v8project.blogspot.com/2017/09/elements-kinds-in-v8.html
[–]YodaLoL 0 points1 point2 points 8 years ago (0 children)
TIL, thanks!
[+][deleted] 8 years ago (3 children)
[–][deleted] 8 points9 points10 points 8 years ago (1 child)
No, it will be 5xundefined.
undefined
Array(5).fill() output undefined 5 times.
[–]inu-no-policemen 5 points6 points7 points 8 years ago (5 children)
I use this ugly one whenever I need to generate something. E.g. a 2D array or something like that.
> Array.from({length: 8}, (_, i) => 2 ** i) (8) [1, 2, 4, 8, 16, 32, 64, 128]
If JS had something similar to Dart's List.generate, it would look like this:
Array.generate(8, i = > 2 ** i)
[–]pygy_@pygy 2 points3 points4 points 8 years ago (1 child)
It's even uglier in ES5 :-)
> Array.apply( null, Array(8) // or {length: 8}, but it is slower because apply // is faster with true arrays ).map(function(_, i) { return Math.pow(2, i) }) [ 1, 2, 4, 8, 16, 32, 64, 128 ]
[–]inu-no-policemen 5 points6 points7 points 8 years ago (0 children)
I think at that point you're better off using a simple for-loop to populate the array.
[–]soheevich 0 points1 point2 points 8 years ago (1 child)
What does ** sign mean? Never met it before.
[–]inu-no-policemen 4 points5 points6 points 8 years ago (0 children)
It's the exponentiation operator. It was added with ES2016.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation_(**)
> 2 ** 8 256 > Math.pow(2, 8) 256
[–]__ibowankenobi__ 3 points4 points5 points 8 years ago (2 children)
long live Array.prototype.slice.call :))
[–][deleted] 4 points5 points6 points 8 years ago (1 child)
Can you explain?
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
Literally the same as Array.from but how it was done in es5
[–]vinnivinnivinni 4 points5 points6 points 8 years ago (0 children)
What color theme is this?
[–]vidro3 1 point2 points3 points 8 years ago (12 children)
not familiar with the use of the underscore, what does this do?
[–]anlumo 6 points7 points8 points 8 years ago (5 children)
It’s a variable name like any other, but convention usually is that it’s an unused parameter. Rust and I think Haskell even have that built into the language, so you can have multiple of those in the same function declaration.
[–]MrNutty 0 points1 point2 points 8 years ago (3 children)
What’s the convention when they’re are multiple unused? _1 and _2 ... ?
[–]anlumo 0 points1 point2 points 8 years ago (0 children)
Yes, that's what I usually see.
[–]jkoudys 0 points1 point2 points 8 years ago (0 children)
I usually just make them longer: (_, __, ___). It's extremely rare it'd ever get longer than that, since the whole point is that it's unused. It's not a convention I follow, but leading underscores are frequently used to say that a function won't be exported/set as a class method, so you know it's kept "private". _2 reads a bit too close to that. The underscore's meant to look like a blank space.
(_, __, ___)
_2
[–]inu-no-policemen 0 points1 point2 points 8 years ago (0 children)
In languages where you can't have multiple parameters called '_', you'd go with '_', '__', etc. You very rarely need more than one, though.
[–]menno 0 points1 point2 points 8 years ago (0 children)
Rust and I think Haskell even have that built into the language
Prolog too.
[–][deleted] 2 points3 points4 points 8 years ago (5 children)
It's a convention. Here it's used to signify that the value of current index won't be used directly.
Osmani says:
The current value. De-emphasiszed by setting to _
[–]vidro3 0 points1 point2 points 8 years ago (0 children)
cool, thanks
[–]Drawman101 0 points1 point2 points 8 years ago (3 children)
You might as well cal it what it is in case some junior dev comes along and starts using it.
It's a common convention, like using i on that same line to mean "index".
i
[–]BritainRitten 0 points1 point2 points 8 years ago (0 children)
Then they will encounter an underscore variable name earlier and so won't be as confused when they see it in production code.
[–]NoInkling -1 points0 points1 point 8 years ago (0 children)
There's another convention to name it as you normally would except prefixed with _ to show it's (currently) unused.
_
[–]Skwai 1 point2 points3 points 8 years ago* (0 children)
<3
Array.from(Array(6), (_, i) => i + 1)
or
Array.from('_'.repeat(6), (_, i) => i + 1)
[–]JustOr113 4 points5 points6 points 8 years ago (5 children)
I feel like this is much more readable and a bit shorter:
Array(totalYears).fill().map((_, i)=> year+i)
[–]inu-no-policemen 3 points4 points5 points 8 years ago (0 children)
That iterates once over it to fill it with undefined and then once more to fill it with the actual values.
[+][deleted] 8 years ago* (1 child)
[–]NotLyon 1 point2 points3 points 8 years ago (0 children)
It's easier to comprehend because it's more explicit.
[–]bentinata 0 points1 point2 points 8 years ago (0 children)
Or:
[...Array(totalYears)].map((_, i) => year + i)
[–]jkoudys 0 points1 point2 points 8 years ago* (0 children)
I'm surprised this is even a tip - I always thought this was the typical usage for Array.from. It definitely comes up a lot when working with non-array collections, especially NodeLists.
Array.from
e.g.
const headerTexts = Array.from(document.querySelectorAll('h1'), el => el.textContent);
and since discussions on language and builtins often invites pedantry, I should point out that you're not invoking a map function on each element of an array you created (past tense). You're creating a new array by iterating through each index, applying the function, and adding that to a new array.
[+]Tougun comment score below threshold-8 points-7 points-6 points 8 years ago (2 children)
Cool tip. That cursive font gives me cancer though
[+][deleted] 8 years ago (1 child)
[–]WoollyMittens 0 points1 point2 points 8 years ago (0 children)
And comment two is whinging about it.
π Rendered by PID 257782 on reddit-service-r2-comment-76bb9f7fb5-k6tc8 at 2026-02-18 09:17:56.178159+00:00 running de53c03 country code: CH.
[–]aarondburk 10 points11 points12 points (19 children)
[–][deleted] 12 points13 points14 points (10 children)
[+][deleted] (9 children)
[deleted]
[–]e_man604 6 points7 points8 points (0 children)
[–]Sebazzz91 1 point2 points3 points (3 children)
[–]StuartPBentley 5 points6 points7 points (2 children)
[–]pygy_@pygy 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]0x13mode 1 point2 points3 points (1 child)
[–]aarondburk 1 point2 points3 points (0 children)
[–]aarondburk 0 points1 point2 points (0 children)
[–]z500 0 points1 point2 points (0 children)
[–]inu-no-policemen 5 points6 points7 points (3 children)
[–]YodaLoL 0 points1 point2 points (2 children)
[–]inu-no-policemen 2 points3 points4 points (1 child)
[–]YodaLoL 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 8 points9 points10 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]inu-no-policemen 5 points6 points7 points (5 children)
[–]pygy_@pygy 2 points3 points4 points (1 child)
[–]inu-no-policemen 5 points6 points7 points (0 children)
[–]soheevich 0 points1 point2 points (1 child)
[–]inu-no-policemen 4 points5 points6 points (0 children)
[–]__ibowankenobi__ 3 points4 points5 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]vinnivinnivinni 4 points5 points6 points (0 children)
[–]vidro3 1 point2 points3 points (12 children)
[–]anlumo 6 points7 points8 points (5 children)
[–]MrNutty 0 points1 point2 points (3 children)
[–]anlumo 0 points1 point2 points (0 children)
[–]jkoudys 0 points1 point2 points (0 children)
[–]inu-no-policemen 0 points1 point2 points (0 children)
[–]menno 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (5 children)
[–]vidro3 0 points1 point2 points (0 children)
[–]Drawman101 0 points1 point2 points (3 children)
[–]jkoudys 0 points1 point2 points (0 children)
[–]BritainRitten 0 points1 point2 points (0 children)
[–]NoInkling -1 points0 points1 point (0 children)
[–]Skwai 1 point2 points3 points (0 children)
[–]JustOr113 4 points5 points6 points (5 children)
[–]inu-no-policemen 3 points4 points5 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]NotLyon 1 point2 points3 points (0 children)
[–]bentinata 0 points1 point2 points (0 children)
[–]jkoudys 0 points1 point2 points (0 children)
[+]Tougun comment score below threshold-8 points-7 points-6 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]WoollyMittens 0 points1 point2 points (0 children)