Languages
[Edit]
EN

JavaScript - initialize array length

0 points
Created by:
a_horse
538

In this article, we would like to show you how to initialize array length in JavaScript.

1. Using Array() constructor

Edit

In this example, we use Array() constructor to initialize an array of a given length (3) with empty values that we can't iterate on.

2. With apply() and map() method

Edit

In this example, we use Array apply() with map() method to initialize an array of a given length with undefined values that we can iterate on.

or initialized with indexes:

3. Using from() method with values initialization

Edit

In this example, we use Array from() method to initialize array lenght and values with indexes.

or all elements initialized with the same (given) value:

References

Edit
  1. Array() constructor - JavaScript | MDN
  2. Function.prototype.apply() - JavaScript | MDN
  3. Array.prototype.map() - JavaScript | MDN
  4. Array.from() - JavaScript | MDN
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join