Descriptionbox icon

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.

Arrays cannot use strings as element indexes (as in an associative array) but must use integers. Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection. The array's object properties and list of array elements are separate, and the array's traversal and mutation operations cannot be applied to these named properties.

Common operationsbox icon

  • code iconCreate an Array
  • code iconAccess an Array item using the index position
  • code iconLoop over an Array
  • code iconAdd an item to the end of an Array
  • code iconRemove an item from the end of an Array
  • code iconAdd an item to the beginning of an Array
  • code iconFind the index of an item in the Array
  • code iconRemove an item by index position
  • code iconRemove items from an index position
  • code iconCopy an Array
  • Accessing array elements box icon

    JavaScript arrays are zero-indexed. The first element of an array is at index 0, and the last element is at the index value equal to the value of the array's length property minus 1. Using an invalid index number returns undefined.

    Array elements are object properties in the same way that toString is a property (to be specific, however, toString() is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid:

    There is nothing special about JavaScript arrays and the properties that cause this. JavaScript properties that begin with a digit cannot be referenced with dot notation and must be accessed using bracket notation.

    For example, if you had an object with a property named 3d, it can only be referenced using bracket notation.

    In the 3d example, '3d' had to be quoted (because it begins with a digit). But it's also possible to quote the array indexes as well (e.g., years['2'] instead of years['2']), although it's not necessary.

    The 2 in years[2] is coerced into a string by the JavaScript engine through an implicit toString conversion. As a result, '2' and '02' would refer to two different slots on the years object, and the following example could be true:

    Relationship between length and numerical propertiesbox icon

    A JavaScript array's length property and numerical properties are connected.

    Several of the built-in array methods (e.g., join(), slice(), indexOf(), etc.) take into account the value of an array's length property when they're called.

    When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length property accordingly:

    Increasing the length.

    Decreasing the length property does, however, delete elements.

    This is explained further on the Array.length page.

    Creating an array using the result of a matchbox icon

    The result of a match between a RegExp and a string can create a JavaScript array. This array has properties and elements which provide information about the match. Such an array is returned by RegExp.exec(), String.match(), and String.replace().

    To help explain these properties and elements, see this example and then refer to the table below: