Select Page

JavaScript Basic Array Methods [Article]

Dustin Usey
Published: October 17, 2022

Arrays are used to store data. This data could be things like strings or numbers for example. Sometimes we need to update or manipulate our arrays by adding or removing data or combining one array with another. In this guide, I’ll go over just some of the many basic JavaScript array methods we have available to us. Methods we’ll discuss include: .length(), .push(), .pop(), .shift(), .unshift(), .indexOf(), .toString(), .join() & .concat()

I’ll include links to our JavaScript Arrays course as well as MDN docs for each of these methods so you can get an even better understanding.

Treehouse Video – JavaScript Arrays

MDN – Arrays
MDN – .length()
MDN – push()
MDN – .pop()
MDN – .shift()
MDN – .unshift()
MDN – .indexOf()
MDN – .toString()
MDN – .join()

MDN – .concat()

If you’d like to follow along, you can do so by running these methods in your browser’s console. Let’s get started!


Let’s start off with an easy method; .length(). This method will return the length of an array. To use it, write your array name and then add the method name. In this example, we’ll run users.length in the console:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
// returns 4


Adding an item to an array is easy. Using the .push() method allows us to add a new item to the end of an array. Checkout the example below. We’ll add a new name to our users array.

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
// returns ['Brandon', 'Keeli', 'Tiffany', 'Steve', 'Shane']


Removing items from an array is also just as easy. Running .pop() on an array will remove the last item of that array. What’s cool about .pop() is that it can also return the item that was removed. Checkout the code snippet below:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
// returns ['Brandon', 'Keeli', 'Tiffani'];
const removedUser = users.pop();
// returns 'Steve'


What if you wanted to remove the first item in your array? That’s where .shift() comes in. Running this method on an array will not only remove the first item of that array, but just like .pop() it returns that item as well:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
// returns ['Keeli', 'Tiffany', 'Steve']
const removedUser = users.shift();
//returns 'Brandon'


The .unshift() method basically does the opposite of .shift(). Instead of removing an item from the beginning of an array, it adds an item to the beginning. Check this out:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
//returns ['Dustin', 'Brandon', 'Keeli', 'Tiffany', 'Steve']


Need to find the index of a specific item in your array? You can run .indexOf() on an array with the value of the item you want to know’s index. Let’s say we wanted to know the index of Keeli from our user’s array. Take a look at the code:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
//returns 1
(since arrays are indexed at 0, "Keeli" is at the 1st index of our array)


Ever needed to return all your array items as a string? Easy, just run .toString() on your array like this:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
//returns 'Brandon,Keeli,Tiffany,Steve'


Sometimes when turning your array items to a string requires a bit more flexibility. In our above example for the .toString() method, you see we get back our array with all the strings with a comma between each array item. If you wanted to format that differently, you could use the.join() method:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
console.log(users.join(', '));
//returns 'Brandon, Keeli, Tiffany, Steve'

As you can see, whatever parameter we give our .join() method, will separate the array items when it converts it to a string. Pretty neat!


Consider the following two arrays:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
const newUsers = ['Yamil', 'Beth', 'Caleb'];

If we wanted to join these two arrays together to create a new array, how could we do this? 🤔 This is where .concat() comes in handy:

const users = ['Brandon', 'Keeli', 'Tiffany', 'Steve'];
const newUsers = ['Yamil', 'Beth', 'Caleb'];
let userList = users.concat(newUsers);
//returns ['Brandon', 'Keeli', 'Tiffany', 'Steve', 'Yamil', 'Beth', 'Caleb']

We created a new variable; userList and this holds the returned value of users.concat(newUsers). Which is a list of array items from both arrays.

I hope this guide on basic array methods was helpful. Remember, these are just some of the many basic JavaScript array methods. Be sure to refer back to the beginning of this post for a list of all the methods covered in this guide. Below, I’ll link a few more methods if you’re interested in learning even more. Have fun and happy coding! 😃

Treehouse Video – JavaScript Arrays

MDN – .reverse()

MDN – .sort()

MDN – .slice()

MDN – .some()