Arrays

Arrays are lists of ordered, stored data. They can hold items that are of any data type. Arrays are created by using square brackets, with individual elements separated by commas.

// An array containing numbers
const numberArray = [0, 1, 2, 3];
// An array containing different data types
const mixedArray = [1, 'chicken', false];

Creating an Array

Using an array literal is the easiest way to create a JavaScript array.

var cars = ['Saab', 'Volvo', 'BMW'];

Spaces and line breaks are not important. A declaration can span multiple lines:

var cars = ['Saab', 'Volvo', 'BMW'];

Using the JavaScript Keyword new is another way to create an array.

var cars = new Array('Saab', 'Volvo', 'BMW');

Access the Elements of an Array

JavaScript array elements are arranged by index values, starting at 0:

  • Index 0 has the first element.
  • Index 1 has the second element.
  • Index 2 has the third element.
  • …and so on.

Individual elements in the array can be accessed using the array name and the element’s index surrounded by square brackets.

array[index]

This code accesses the value of the first element in the cars array:

var cars = ['Saab', 'Volvo', 'BMW'];
console.log(cars[0]);
// Output: Saab

Array elements can be changed by accessing the element and assigning a new value to it.

var cars = ['Saab', 'Volvo', 'BMW'];
cars[1] = 'Tesla';
console.log(cars[1]);
// Output: Tesla

Arrays

.concat()
Merges, or concatenates, two or more arrays.
.filter()
Creates a new array with all elements that pass the test from the provided function.
.find()
Returns the first element in the array that satifies the given function.
.findIndex()
Returns the first index that passes the callback function's test. Returns -1 if no element passes the test.
.forEach()
Loops over the array, passing each item in the array into the callback function provided.
.indexOf()
Returns the first index at which an element can be found. Returns -1 if element is not found.
.isArray()
Returns true for arrays, otherwise false.
.join()
Elements of an array are converted to strings and concatenated together, returning the resulting string.
.lastIndexOf()
Returns the last index at which an element can be found. Returns -1 if element is not found.
.length
Returns the specific number of elements in the array.
.map()
Creates a new array with the results of calling a function for every element in array.
.pop()
Removes the last element of an array, decrements the array length, and returns the value that it removed.
.push()
Adds one or more elements to end of array and returns new length.
.reduce()
Combines each element of an array, using a reducer function you specify, and returns a single value.
.reverse()
Reverses the order of the elements of an array in place and returns the reversed array.
.shift()
Removes and returns the first element of the array. All subsequent elements will shift down one place.
.slice()
Returns a shallow copy of part of array, while original array is not modified.
.sort()
Sorts the elements of an array in place and returns the sorted array.
.splice()
Modifies array by inserting, deleting, and/or replacing array elements then returns an array of deleted elements.
.toString()
Returns a string with each of the array values, separated by commas. Does not mutate the original array.
.unshift()
Adds one or more elements to beginning of array and returns new length.
.valueOf()
Returns the value of all the elements of the original array.
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy