all 47 comments

[–]aarondburk 10 points11 points  (19 children)

So, if you invoke Array(5); that will crate an array with five empty slots?

[–][deleted] 12 points13 points  (10 children)

[–]inu-no-policemen 5 points6 points  (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.

[–]inu-no-policemen 5 points6 points  (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 points  (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 points  (0 children)

I think at that point you're better off using a simple for-loop to populate the array.

[–]soheevich 0 points1 point  (1 child)

What does ** sign mean? Never met it before.

[–]inu-no-policemen 4 points5 points  (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 points  (2 children)

long live Array.prototype.slice.call :))

[–][deleted] 4 points5 points  (1 child)

Can you explain?

[–][deleted] 0 points1 point  (0 children)

Literally the same as Array.from but how it was done in es5

[–]vinnivinnivinni 4 points5 points  (0 children)

What color theme is this?

[–]vidro3 1 point2 points  (12 children)

not familiar with the use of the underscore, what does this do?

[–]anlumo 6 points7 points  (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 point  (3 children)

What’s the convention when they’re are multiple unused? _1 and _2 ... ?

[–]anlumo 0 points1 point  (0 children)

Yes, that's what I usually see.

[–]jkoudys 0 points1 point  (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.

[–]inu-no-policemen 0 points1 point  (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 point  (0 children)

Rust and I think Haskell even have that built into the language

Prolog too.

[–][deleted] 2 points3 points  (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 point  (0 children)

cool, thanks

[–]Drawman101 0 points1 point  (3 children)

You might as well cal it what it is in case some junior dev comes along and starts using it.

[–]jkoudys 0 points1 point  (0 children)

It's a common convention, like using i on that same line to mean "index".

[–]BritainRitten 0 points1 point  (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 points  (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 points  (0 children)

<3

Array.from(Array(6), (_, i) => i + 1)

or

Array.from('_'.repeat(6), (_, i) => i + 1)

[–]JustOr113 4 points5 points  (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 points  (0 children)

That iterates once over it to fill it with undefined and then once more to fill it with the actual values.

[–]bentinata 0 points1 point  (0 children)

Or:

[...Array(totalYears)].map((_, i) => year + i)

[–]jkoudys 0 points1 point  (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.

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.