THE ARRAY TYPE
After the Object
type, the Array
type is probably the most used in ECMAScript. An ECMAScript array is very different from arrays in most other programming languages. As in other languages, ECMAScript arrays are ordered lists of data, but unlike in other languages, they can hold any type of data in each slot. This means that it's possible to create an array that has a string in the first position, a number in the second, an object in the third, and so on. ECMAScript arrays are also dynamically sized, automatically growing to accommodate any data that is added to them.
Creating Arrays
Arrays can be created in several basic ways. One is to use the Array
constructor, as in this line:
let colors = new Array();
If you know the number of items that will be in the array, you can pass the count into the constructor, and the length
property will automatically be created with that value. For example, the following creates an array with an initial length
value of 20:
let...