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:
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
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 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
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, ...) | 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, ...) | 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];
charles.sort();
console.log(charles);
[350, 23, 47, 'Tech', 'coder']
['coder', 'Tech', 23, 47, 350]
[23, 47, 350, 'coder', 'Tech']
[23, 350, 47, 'Tech', 'coder']
Which statement converts the string str = 'LaunchCode students rock!'
into the array ['LaunchCode', 'students', 'rock!']
?
str.join(" ");
str.split(" ");
str.join("");
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();
console.log(selectedItems);
['chips', 'cucumbers', 'edamame']
['chips', 'cucumbers', 'edamame', 'milk']
['cheese', 'chips', 'cucumbers']
['cheese', 'chips', 'cucumbers', 'edamame']