Learn about some useful methods in Javascript Arrays

In this tutorial we will explore some methods which will help you to do certain operations on Javascript Arrays.

The code for this lecture is available on Github

Consider this string

let fruits = 'Mango, Peach, Banana';

What if we want to separate out the fruits from this String i.e. get an array of fruits.

To do this we can use the split() method.

In order to use split() we need to pass the character which needs to be used to perform the split.

let fruitArray = fruits.split(',');
fruitArray // ['Mango', 'Peach', 'Banana']
fruitArray[0] // Mango

To combine all the array elements back, so that we can get a String, we can use the join() method.


fruitArray.join('-') // 'Mango-Peach-Banana'

Alternatively we can use the toString() method.

How to add and remove elements to or from an Array?

To add or remove an element at the end of the Array you can use push() and pop()

push() lets us add an element to the end of the Array.


pop() lets us remove an element from the end of the Array.


Similarly, to add or remove an element at the start of the Array you can use unshift() and shift()

Arrays in Javascript – The Basics

What are Arrays ?

Arrays can be used to store a list of data items. Like a list of numbers or objects.

let ageList = [15, 17, 25, 16, 14]

So if we do not use arrays then we need to declare separate variables for all the ages. Imagine if we had 100 people. Declaring 100 variables will be very inefficient.

Each element in an array is stored at an index. Indexes start from 0.

Index : 0 1 2 3 4
Value : 15 17 25 16 14

How to access elements in an array ?

It is easy to access the elements stored in the array using index.

Like if we want to get element stored at the 3rd location we can use


Similarly we can assign new value to an index

ageList[3] = 32

How to find number of items in the array ?

To find the length of array or number of items in the array we can use the length property.