Array

Array – Creates an array.

Description

new Array([mixed params, …])

Parameters

Name Description Type Default Optional
params, … mixed Yes

Examples

Example #1 – array example

Two ways of creating an array.

var array = new Array(1, 2, 3); console.log(array); // [1, 2, 3] console.log(typeof array); // object console.log(array.length); // 3 array[3] = 4; console.log(array); // [1, 2, 3, 4]
var array = [1, 2, 3]; console.log(array); // [1, 2, 3] console.log(typeof array); // object console.log(array.length); // 3 array[3] = 4; console.log(array); // [1, 2, 3, 4]
Example #2 – array example
var array = [1, 2, 3]; array[4] = 5; console.log(array); // [1, 2, 3, 4: 5] console.log(array.length); // 11 console.log(array[3]); // undefined array[3] = 4; console.log(array); // [1, 2, 3, 4, 5]
Example #3 – array example

Setting initial size of an array. See Array.of to set as value instead.

var array = new Array(10); console.log(array); // [] console.log(array.length); // 10
Example #4 – array example
var array = [1, 2, 3, 4]; console.log(array); // [1, 2, 3, 4] var first = array[0]; console.log(first); // 1 var last = array[array.length - 1]; console.log(last); // 4 console.log(array[10]); // undefined

External references