8.3. 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.

8.3.1. 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.

Methods That Return Information About The Array
Method Syntax Description
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
Method Syntax Description
reverse arrayName.reverse() Reverses the order of the elements in an array.
sort arrayName.sort() Arranges the elements of an array into increasing order (kinda).
Methods That Add Or Remove Entries From An Array
Method Syntax Description
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, item1, item2, ...) 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
Method Syntax Description
concat arr.concat(otherArray1, otherArray2, ...) Combines two or more arrays and returns the result as a new array.
join arr.join('connecter') Combines all the elements of an array into a string.
slice arr.slice(start index, end index) Copies selected entries of an array into a new array.
split stringName.split('delimiter') Divides a string into smaller pieces, which are stored in a new array.

8.3.2. 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.

Question

What is printed by the following code?

1let charles = ['coder', 'Tech', 47, 23, 350];
2charles.sort();
3console.log(charles);
  1. [350, 23, 47, 'Tech', 'coder']
  2. ['coder', 'Tech', 23, 47, 350]
  3. [23, 47, 350, 'coder', 'Tech']
  4. [23, 350, 47, 'Tech', 'coder']

Question

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("");

Question

What is printed by the following program?

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