Creating arrays

There are several equivalent ways to create an array.

Loading TypeScript...

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.

Loading TypeScript...

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.

Loading TypeScript...

Checking for an array

You can check if a value is an array with the Array.isArray function.

Loading TypeScript...

The fill function can fill each element of an empty array with the same value.

Loading TypeScript...

You can also pass an optional start and end index to fill to control which elements are assigned.

Loading TypeScript...

Was this page helpful?