JavaScript: Remove Element from an Array

Remove element or object from an array in javascript. Here we will explain how to remove the last element from array javascript, remove the first element from array javascript, remove specific elements from array javascript with different techniques and examples.

This tutorial demonstrates different techniques and methods with examples to removing the first, last or specific index elements from an array in javascript.

javaScript Methods: To Remove Element From Array javaScript

This tutorial demonstrates an easy way for you. To remove the first, last specific index element from array in javascript:

1. First Method – Array Shift() JavaScript

Use the javascript array shift() method, which is used to remove the first element of the array javascript.

Let’s take an example to remove the first element from an array javascript using array shift method:

 var array = ["bar", "baz", "foo", "qux"];
 array.shift();
 
 console.log( array );

Result of the above example:

After removed the first element from an array using javascript shift method. The output will look like:

["baz", "foo", "qux"]

2. Second Method:- Array pop() javascript

Using the javaScript pop method, which is used to remove the last element of the array javascript, returns that new element array.

 var arr = ["bar", "baz", "foo", "qux"];
 
arr.pop();
 
console.log( arr );

Result of the above example

After removed the last element from an array using javascript pop method. The output will look like:

["bar", "baz", "foo"]

3. Third Method:- Array splice() javascript

Use the javascript splice() method, that is used to add or remove element or item from an array javascript.

Let’s take an example to remove specific index elements from array javascript. In this example, we will remove 1 to 2 index values from the array.

If you want to want to know more about javascript Array Splice Method, Read Here

  var arr = ["bar", "baz", "foo", "qux"];

  arr.splice(1, 2);

  console.log(arr);

Result of the above example

["bar", "qux"]

If you want to know more about javascript array methods, you may like

#javascript #programming

JavaScript: Remove Element from an Array
3.10 GEEK