JavaScript Array Shift(), Unshift(), Push() and Pop() Methods

JavaScript array manipulation; Through this tutorial, i am going to show you how to add and remove first and last elements from the javascript array with the help of examples.

Array Shift(), Unshift(), Push() and Pop() Methods in JavaScript

There are four built-in methods in JavaScript to remove or add the last and first elements from the array; as shown below:

  • unshift(): Add elements to the beginning of an array.
  • shift(): Remove an element/item from the beginning of an array.
  • push(): Add elements/items to the end of an array.
  • pop(): Remove an element/item from the end of an array.

1 – unshift() Method JavaScript

The javaScript unshift() method adds new items to the first or beginning of an array. and it will return the new length of array.

Using the javascript unshift() method to add elements or items first or beginning array in javascript.

Syntax of JavaScript unshift() method

 array.unshift(element1, element2, ..., elementX)

Parameter of JavaScript unshift() method

ParameterDescription
element1, element2, …, elementX Ii is required. The element(s) to add to the beginning of the array

Example – add element start or beginning of an array javascript

Let, you have an array that’s the name arrNum, and it contains four elements inside it, see below:

var arrNum = [
   "one",
   "two",
   "three",
   "four"
 ];

If you want to add element start or beginning of an arrNum array. So you can use the unshift() method of javaScript like below:

var arrNum = [
   "one",
   "two",
   "three",
   "four"
 ];
 arrNum.unshift("zero");
 console.log( arrNum );

The result of the above example is:

["zero", "one", "two", "three", "four"]

2 – shift() Method JavaScript

The JavaScript shift () method, which is used to remove the first or beginning element of the array javascript.

Using the javascript shift to remove the first or beginning element of an array javascript.

Syntax of JavaScript shift() method

 array.shift()

Note:- No parameter is required

Example – javascript remove the element to first or beginning of an array

Let, you have an array that’s the name arr, and it contains ten elements inside it, see below:

 var arr = [ 
     "one", 
     "two", 
     "three", 
     "four", 
     "five",
     "six", 
     "seven", 
     "eight", 
     "nine", 
     "ten" 
 ]; 

If you want to remove element start or beginning of an arr array. So you can use the unshift() method of javaScript like below:

 var arr = [ 
     "one", 
     "two", 
     "three", 
     "four", 
     "five",
     "six", 
     "seven", 
     "eight", 
     "nine", 
     "ten" 
 ]; 
 arr.shift();
 console.log( arr );

Result of the above example

 ["two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
 

3 – push() Method JavaScript

The javaScript push() method is used to add a new element to the last or end of an array.

Using the javascript push() method to add the elements or items from the last or end of an array.

Syntax of JavaScript push() method

 array.push( element1, element2, ..., elementX )

Parameters of JavaScript push() method

ParameterDescription
element1, element2, …, elementX Ii is required. The element(s) to add to the array

Example – javascript add the element to last or end of an array

Let, you have an array that’s the name arrNum, and it contains four elements inside it, see below:

var arrNum = [
   "one",
   "two",
   "three",
   "four"
 ];

If you want to add the single item into the arryNum array. So you can use the push() method of javaScript like below:

var arrNum = [
   "one",
   "two",
   "three",
   "four"
 ];
 arrNum.push("five");
 console.log( arrNum );

The result of the above example is:

["one", "two", "three", "four", "five"]

If you want to see more example of javascript push() method, you can click and see here

4 – pop() Method JavaScript

The javaScript pop method removes the last or end element of an array, returns a new element. This method changes the length of the array.

Using the javascript pop() method to remove the elements or items from the last or end of an array.

Syntax of JavaScript pop method

 array.pop()

Note:- No parameter is required.

Example – javascript remove the element to last or end of an array

Let, you have an array that’s the name arr, and it contains ten elements inside it, see below:

 var arr = [ 
     "one", 
     "two", 
     "three", 
     "four", 
     "five",
     "six", 
     "seven", 
     "eight", 
     "nine", 
     "ten" 
 ]; 

If you want to remove element last or end of an arr array. So you can use the pop() method of javaScript like below:

 var arr = [ 
      "one", 
      "two", 
      "three", 
      "four", 
      "five",
      "six", 
      "seven", 
      "eight", 
      "nine", 
      "ten" 
  ]; 
 arr.pop();
 console.log( arr );

Result of the above example

["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
 

More Javascript Tutorials

Recommended:-JavaScript Arrays

Leave a Comment