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

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

In this tutorial, you will learn javascript array methods, which are used to remove or add the last or end or first or beginning elements from the javascript array.

When we work with JavaScript arrays. Sometimes we need to add or remove elements from the end and beginning of a JavaScript array. So don’t worry, there are four amazing ways in JavaScript to remove or add the beginning or starting elements of JavaScript.

There are four inbuilt methods in JavaScript to remove or add the last and first elements from the array are:-

In this tutorial, we will show you four methods with its definition, syntax, parameters with examples to add element or item first or beginning and last or ending of an array javascript.

1: unshift() Method JavaScript

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

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

Syntax of unshift() method is

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

Parameter with description

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

Consider, we 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

You can use the javascript shift to remove the first or beginning element of an array javascript.

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

Syntax of the shift() method is

 array.shift()

Note:- No parameter is required

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

Consider, we 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

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

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

Syntax of push() method is

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

Parameter with description

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

Consider, we 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

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

Definition: – 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.

Syntax of the pop method is

 array.pop()

Note:- No parameter is required.

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

Consider, we 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"]
 

Recommended Javascript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *