Arrays in JavaScript are really just Objects with properties whose names happen to be non-negative integers. They also have one very special property: length.
A new array has a length of zero.
var a = []; // a.length == 0
Whenever a property whose name happens to be a non-negative integer is set, the length is automatically set to one more than that integer (unless the length is already greater than that integer).
a[3] = true; // a.length == 4
a[9] = false; // a.length == 10
a[6] = false; // a.length == 10
JavaScript Arrays are “sparse” – no space is allocated for unused elements.
// a[2] === undefined, (2 in a) === false
Deleting an item in the array has no effect on the length.
delete a[9]; // a.length == 10
Explicitly enlarging the length of the array has no effect on the items in the array.
a.length = 12; // a[11] === undefined, (11 in a) === false
Explicitly reducing the length of the array automatically deletes any items in the array whose index is greater than or equal to the length.
a.length = 5; // a[6] === undefined, (6 in a) === false
You can use the length to add an item to an array (but prefer the push method).
a[a.length] = true; // a[5] === true, a.length == 6
Arrays support a number of useful methods that treat the object like a traditional array:
push, pop: Adds or removes items from the end of the array.
unshift, shift: Adds or removes items from the beginning of the array.
slice: Returns a range of array items as a new array.
splice: Inserts and/or removes items to/from the array.
concat: Combines one or more arrays to form a new, concatenated array.
join: Joins the string forms of the array items into one string.
sort: Sorts the items in the array.
reverse: Reverses the items in the array.
There’s no clone method, but you can clone an array by calling either slice or concat with no arguments.