Creating arrays
There are several equivalent ways to create an array.
If you pass a single number into new Array
, you create an empty array of that size. If you pass more than one number into new Array
, it creates a new array with those values. Effectively, the first argument might do something different depending on how many arguments are passed in.
Array.from
is especially useful for converting a wide range of iterable objects into arrays. It also allows you to define a mapping function which is applied to each element of a source array.
Checking for an array
You can check if a value is an array with the Array.isArray
function.
The fill
function can fill each element of an empty array with the same value.
You can also pass an optional start
and end
index to fill
to control which elements are assigned.