JavaScript Remove First and Last Element from Array

JavaScript remove element from array; Through this tutorial, i am going to show you how to remove first, last and specific element from array in JavaScript with examples.

Remove Element From Array in javaScript

Use the following methods to remove the first, last and specific index element from an array in javaScript:

  • To remove first element from array in javaScript using Shift()
  • To remove last element from array in javascript using pop()
  • To remove specific element from array in javascript using splice()

To remove first element from array in javaScript using Shift()

Using the javaScript shift() method, you can remove the first element of the array javascript.

Here, i will take an example for remove the first element from an array javascript using array shift method; as shown below:

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

Output of the above program:

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

To remove last element from array in javascript using pop()

Using the javaScript pop method, You can remove the last element of the array javaScript.

Here, i will take second example for remove the last element of the array javaScript; as shown below:

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


Output of the above program:

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

To remove specific element from array in javascript using splice()

Using javascript splice() method, you can remove elements from an array in javascript.

Here, i will take an example for remove specific index elements from array javaScript; as shown below:

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

Output of the above code:

  ["one", "four", "five", "six", "seven", "eight", "nine", "ten"]
 

More JavaScript Tutorials

Be First to Comment

Leave a Reply

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