Arrays

An array is an object that stores an ordered list of elements at indexes 0 to length - 1. Keys that are valid array indexes count as elements; any other key is an ordinary property, and length is always one more than the highest index. Create arrays with the literal [...], and nest them for grids (grid[1][2]).

Elements, properties, holes and lengthJavaScriptLive
const arr = ['Sophia', 25];
arr.note = 'VIP';     // a property, not an element
arr[4] = true;        // an element: length jumps to 5, leaving holes
console.log(arr, arr.length);
arr.length = 1;       // assigning length truncates
console.log(arr, typeof arr, Array.isArray(arr));

Holes (empty slots, also written [0, , 2]) differ from undefined elements: forEach(), map() and filter() skip them, while for...of, spread and the ES2023 copying methods read them as undefined.

Subsections