Array Methods

As with strings, JavaScript provides us with useful methods for arrays. These methods will either alter an existing array, return information about the array, or create and return a new array.

Common Array Methods

Here is a sample of the most frequently used array methods. More complete lists can be found here:

  1. W3 Schools Array Methods .
  2. MDN Web Docs .

To see detailed examples for a particular method, control-click (or right-click) on its name to open the view in a new tab.

Methods That Return Information About The Array

.includes()arrayName.includes(item)Checks if an array contains the specified item.
.indexOf()arrayName.indexOf(item)Returns the index of the first occurrence of an item in the array. If the item is not in the array, -1 is returned.

Methods That Rearrange The Entries In The Array

.reverse()arrayName.reverse()Reverses the order of elements in an array.
.sort()arrayName.sort()Arranges elements of an array in increasing order (kinda).

Methods That Add Or Remove Entries From An Array

.pop()arrayName.pop()Removes and returns the LAST element in an array.
.push()arrayName.push(item1, item2, ...)Adds one or more items to the END of an array and returns the new length.
.shift()arrayName.shift()Removes and returns the FIRST element in an array.
.splice()arrayName.splice(index, number, ...)Adds, removes, or replaces one or more elements anywhere in the array.
.unshift()arrayName.unshift(item1, item2, ...)Adds one or more items to the START of an array and returns the new length

Methods that Create New Arrays

.concat()arr.concat(otherArray1, ...)Combines two or more arrays into a new array.
.join()arr.join('connecter')Combines all elements of an array into a string.
.slice()arr.slice(start index, end index)Copies selected entries of an array to a new array.
.split()stringName.split('delimiter')Divides a string into smaller pieces stored in a new array.

Check Your Understanding

Follow the links in the table above for the sort, slice, split and join methods. Review the content and then answer the following questions.


What is printed by the following code?

let charles = ['coder', 'Tech', 47, 23, 350];
  1. [350, 23, 47, 'Tech', 'coder']
  2. ['coder', 'Tech', 23, 47, 350]
  3. [23, 47, 350, 'coder', 'Tech']
  4. [23, 350, 47, 'Tech', 'coder']

Which statement converts the string str = 'LaunchCode students rock!' into the array ['LaunchCode', 'students', 'rock!']?

  1. str.join(" ");
  2. str.split(" ");
  3. str.join("");
  4. str.split("");

What is printed by the following program?

let groceryBag = ['bananas', 'apples', 'edamame', 'chips', 'cucumbers', 'milk', 'cheese'];
let selectedItems = [];

selectedItems = groceryBag.slice(2, 5).sort();
  1. ['chips', 'cucumbers', 'edamame']
  2. ['chips', 'cucumbers', 'edamame', 'milk']
  3. ['cheese', 'chips', 'cucumbers']
  4. ['cheese', 'chips', 'cucumbers', 'edamame']