Working with Arrays
Arrays are another complex object type built on top of objects. Unlike objects, arrays are designed to work with lists of data. Arrays may be created in several ways. The first is known as an Array literal and, similarly to object literals, is simply a means of passing a defined Array value to a variable:
var myArray = [1, 2, 3]; var myEmptyArray = [];
The values of an array have no keys, and are instead accessed using integer indexes with the square bracket form:
myValue = myArray[3];
As with other types, the array type also has a constructor function that's used to create array instances. The array constructor can be passed values to prepopulate the Array
. Therefore, the following examples are equivalent:
var arr1 = [1, 2, 3]; var arr2 = new Array(1, 2, 3);
However, when using the constructor form, passing a single integer value will create an array with a set number of values set to undefined:
var arr = new Array(3); console.log( arr...