JavaScript Array Push, Pop, Shift and Unshift Methods

Last Updated on by in JavaScript

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

console.log(array);

// 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: 

array.pop()

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

console.log(names); 
//Output: ["Blaire", "Ash", "Coco", "Dean"]

console.log(remove); 
//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();

console.log(names);
// Output: ["Ash", "Coco", "Dean", "Georgia"]

console.log(initialElement);
// 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')

console.log(nameArray);
//Output: ["Willy", "Blaire", "Ash", "Coco", "Dean", "Georgia"]

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