JavaScript Array Push, Pop, Shift and Unshift Methods with Examples

Article By Rawat Digamber on

JavaScript array push

JavaScript Array push()

array.push(): We use JavaScript array push method to push one or more values into an array. As you can see, the length of the array will be altered thanks to this function.

Let’s take a look at the syntax of the JavaScript push function below.

// Syntax: 

array.push(element1[, ...[, elementN]])

Arguments: Let’s take a look at the .push() JavaScript arguments. As for the number of arguments permitted in JavaScript array push function, there is no limit as such. It is about the number of elements you want to insert into the array using push JavaScript.

Return value: JavaScript array push function will be returning the new length of the array after you are done with inserting arguments.

JavaScript Array Push Function Examples

// Example JavaScript push method
var array = [];

array.push(10, 20, 30, 40, 50, 60, 70);


// Output: [10, 20, 30, 40, 50, 60, 70]

JavaScript Array pop()

Array.pop(): We use pop JavaScript to remove the last element in an array. Moreover, this function returns the removed element. At the same, it will reduce the length of the array by one. This function is the opposite of the JavaScript array push function.

Let’s have a look at the syntax of this function below.

// Syntax: 


Arguments: This function does not pass any arguments.

Return value: As stated before, this function returns the removed element. In case the array is empty, you will be getting undefined as a returned value.

Remove the Last Element of an Array Using pop() Method in JavaScript

The following example produces the names array holding the four values, then popped out the last element in the array.

// Example JavaScript pop() method
var names = ['Blaire', 'Ash', 'Coco', 'Dean', 'Georgia'];

var remove = names.pop();

//Output: ["Blaire", "Ash", "Coco", "Dean"]

//Output: 'Georgia'

As you have noticed in this example, the pop() function returns the last element in the array, which is “Georgia”.

JavaScript Array Shift Method

We use JavaScript array shift() method to remove an element from the beginning of an array. It returns the item you have removed from the array, and length of the array is also changed. Basically, it excludes the element from the 0th position and shifts the array value to downward and then returns the excluded value.

Let’s take a look at an example below:

// Example JavaScript shift() method

var names = ['Blaire', 'Ash', 'Coco', 'Dean', 'Georgia'];

var initialElement = names.shift();

// Output: ["Ash", "Coco", "Dean", "Georgia"]

// Output: Blaire

Unshift JavaScript Method

Now we are going to see the case of unshift() JavaScript function. You will be using this function to add items to the beginning of an array.

// Example JavaScript unshift() method

var nameArray = ['Ash', 'Coco', 'Dean', 'Georgia'];

nameArray.unshift('Willy', 'Blaire')

//Output: ["Willy", "Blaire", "Ash", "Coco", "Dean", "Georgia"]

The unshift() function in JavaScript returns the new array length.

Feel free to contact me, If you are looking for a freelance web and mobile app developer in India, with the following skills: Angular JS, Angular 2+, Node JS, Firebase, MongoDB, Ionic Framework and WordPress.

I also offer remote contracting services to clients across the globe.

Get Started