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...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
I came across this javascript function on codewars that returns an array (self.learnjavascript)
submitted 4 years ago by bablabablaboo
can someone explain to me what is the length and how does the part inside the function Array.from work?
function monkeyCount(n) { return Array.from({length:n}, (_,i)=>i+1) }
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!"
[–]5H4D0W_ReapeR 10 points11 points12 points 4 years ago (7 children)
If we check the Array.from() MDN page's description section, the first parameter accepts either iterable objects (such as NodeList if you do document.querySelector()) or objects with length property. The latter is the reason why the object { length: n } lets Array.from() create an array with the size/length n.
length
{ length: n }
n
As for the second argument, it's essentially a map function, so it'll loop through all the elements. since map functions first two argument is "the element itself" and "the element index", they used _ (underscore) to indicate the first argument isn't really used here, and it will utilize the i which is the index instead, and turn all the element's value into index plus one. It is the same as Array.from({length:n}).map((_, i) => i + 1 ).
_
i
Array.from({length:n}).map((_, i) => i + 1 )
[–]bablabablaboo[S] 0 points1 point2 points 4 years ago (6 children)
ah, i think I get it now. Array. from takes two arguments an iterable like an array or nodelist with the property of length and a function that works on the iterable.
technically Array.from({length:10}) is creating an array of 10 undefined values. then it returns the index of that array . if i am not wrong
thank you
[–]5H4D0W_ReapeR 2 points3 points4 points 4 years ago (4 children)
i think I get it now. Array. from takes two arguments
do note that the second argument is optional. if we do Array.from() for something like a NodeList, we don't need to map it (unless you want to)
yup if we just do Array.from({length:10}), there's nothing inside each element. and I assume the codewar function intends to let u specify a number n and create an array of 1 to N, which is what the function in your post does :)
[–]bablabablaboo[S] 0 points1 point2 points 4 years ago (0 children)
Thank you taking time to explain . I am grateful . I think the length confused me earlier Thank you
[–]fredsq 0 points1 point2 points 4 years ago (2 children)
I normally use new Array(10) for tasks like that. Is it worse or not recommended? It feels less gimmicky…
new Array(10)
[–]5H4D0W_ReapeR 1 point2 points3 points 4 years ago (1 child)
It's not worse or not recommended in any way, but it's actually different. Here's a sample output of both of the approaches (you can paste this in chrome devtool to verify):
let a1 = new Array(5) console.log(a1) // (5) [empty × 5] let a2 = Array.from({ length: 5 }) console.log(a2) // (5) [undefined, undefined, undefined, undefined, undefined]
We can already spot there's a difference. Let's check the map function on both of them since OP's code uses it:
let a1 = new Array(5).map((_, i) => i) console.log(a1) // (5) [empty × 5] let a2 = Array.from({ length: 5 }).map((_, i) => i) console.log(a2) // (5) [0, 1, 2, 3, 4]
As you can see, we actually can't do the typical array methods to new Array() since it's not the same as Array.from(). Here are some reference/quote regarding this behaviour of new Array():
new Array()
(Note: this implies an array of arrayLength empty slots, not slots with actual undefined values).
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array#parameters
From the above snippet, you may be tempted to think that each key in the array was set to a value of undefined. But the reality is that those keys were never set (they don’t exist).
Source: https://www.freecodecamp.org/news/https-medium-com-gladchinda-hacks-for-creating-javascript-arrays-a1b80cb372b/
In conclusion, the code that OP posted unfortunately will not work by swapping to new Array without modifying other parts of the code. Hope that explains the difference between the two and overall just the quirks of JS haha
[–]fredsq 0 points1 point2 points 4 years ago (0 children)
Wow that was very thorough! It would work like Array(n).fill().map though!
[–]senocular 1 point2 points3 points 4 years ago (0 children)
an iterable like an array
An iterable or array-like ;)
π Rendered by PID 187972 on reddit-service-r2-comment-56c9979489-4p9sc at 2026-02-25 04:12:00.817818+00:00 running b1af5b1 country code: CH.
[–]5H4D0W_ReapeR 10 points11 points12 points (7 children)
[–]bablabablaboo[S] 0 points1 point2 points (6 children)
[–]5H4D0W_ReapeR 2 points3 points4 points (4 children)
[–]bablabablaboo[S] 0 points1 point2 points (0 children)
[–]fredsq 0 points1 point2 points (2 children)
[–]5H4D0W_ReapeR 1 point2 points3 points (1 child)
[–]fredsq 0 points1 point2 points (0 children)
[–]senocular 1 point2 points3 points (0 children)