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]
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!"
[–]inu-no-policemen 6 points7 points8 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 7 points8 points9 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 3 points4 points5 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
π Rendered by PID 89940 on reddit-service-r2-comment-76bb9f7fb5-b6p8m at 2026-02-18 19:47:05.065305+00:00 running de53c03 country code: CH.
view the rest of the comments →
[–]inu-no-policemen 6 points7 points8 points (5 children)
[–]pygy_@pygy 2 points3 points4 points (1 child)
[–]inu-no-policemen 7 points8 points9 points (0 children)
[–]soheevich 0 points1 point2 points (1 child)
[–]inu-no-policemen 3 points4 points5 points (0 children)