JavaScript Array Reference

Array Object

The Array object is used to store multiple values in a single variable:

var cars = [“Saab”, “Volvo”, “BMW”];

Array indexes are zero-based: The first element in the array is 0, the second is 1, and so on.

For a tutorial about Arrays, read our JavaScript Array Tutorial.


Array Properties

Property Description
constructor Returns the function that created the Array object’s prototype.

  • For JavaScript arrays the constructor property returns function Array() { [native code] }
  • For JavaScript numbers the constructor property returns function Number() { [native code] }
  • For JavaScript strings the constructor property returns function String() { [native code] }
length Sets or returns the number of elements in an array
prototype Allows you to add properties and methods to an Array object

  • When constructing a property, ALL arrays will be given the property, and its value, as default. When constructing a method, ALL arrays will have this method available.

Note: Array.prototype does not refer to a single array, but to the Array() object itself. Prototype is a global object constructor which is available for all JavaScript objects.

Array Methods

Method Description
concat() Joins two or more arrays, and returns a copy of the joined arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.
copyWithin() Copies array elements within the array, to and from specified positions
entries() Returns a key/value pair Array Iteration Object
every() Checks if every element in an array pass a test. The every() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a false value, every() returns false (and does not check the remaining values)
  • If no false occur, every() returns true
fill() Fill the elements in an array with a static value.It is possible to specify the index for starting and ending fill(). By default it changes the whole array.
filter() Creates a new array with every element in an array that pass a test.

filter() does not execute the function for array elements without values. filter() does not change the original array.

find() Returns the value of the first element in an array that pass a test.

The find() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
  • Otherwise it returns undefined
findIndex() Returns the index of the first element in an array that pass a test. The findIndex() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)
  • Otherwise it returns -1
forEach() Calls a function for each array element

Note: forEach() does not execute the function for array elements without values.

from() Creates an array from an object
includes() Check if an array contains the specified element. This method returns true if the array contains the  element, and false if not.
indexOf() Search the array for an element and returns its position. he search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.
isArray() Checks whether an object is an array. This function returns true if the object is an array, and false if not.
join() Joins all elements of an array into a string. The elements will be separated by a specified separator. The default separator is comma (,).
keys() Returns a Array Iteration Object, containing the keys of the original array
lastIndexOf() Search the array for an element, starting at the end, and returns its position. The search will start at the specified position, or at the end if no start position is specified, and end the search at the beginning of the array.
map() Creates a new array with the result of calling a function for each array element

The map() method calls the provided function once for each element in an array, in order. map() does not execute the function for array elements without values. map() does not change the original array.

pop() Removes the last element of an array, and returns that element. This method changes the length of an array.
push() Adds new elements to the end of an array, and returns the new length.

The push() method adds new items to the end of an array, and returns the new length.  The new item(s) will be added at the end of the array. This method changes the length of the array.

reduce() Reduce the values of an array to a single value (going left-to-right). reduce() does not execute the function for array elements without values.
reduceRight() Reduce the values of an array to a single value (going right-to-left). reduceRight() does not execute the function for array elements without values.
reverse() Reverses the order of the elements in an array
shift() Removes the first element of an array, and returns that element.

Note: This method changes the length of the array. The return value of the shift method is the removed item.

slice() Selects a part of an array, and returns the new array. The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.
some() Checks if any of the elements in an array pass a test

The some() method checks if any of the elements in an array pass a test (provided as a function). The some() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)
  • Otherwise it returns false
sort() Sorts the elements of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down).

By default, the sort() method sorts the values as strings in alphabetical and ascending order.

splice() Adds/Removes elements from an array.This method changes the original array.
toString() Converts an array to a string, and returns the result. The returned string will separate the elements in the array with commas.
unshift() Adds new elements to the beginning of an array, and returns the new length. This method changes the length of an array.
valueOf() Returns the primitive value of an array. This method is the default method of the array object. Array.valueOf() will return the same as Array