Javascript programming articles and tutorials for beginner | Anjan Dutta

How to remove element from array in javascript

How to remove element from array in javascript

There are many ways of removing elements from an array. Each of these methods has different use cases.

Remove element from the end of an array.

Array pop - Remove and return element from the end of an array

var arr = [10, 20, 30, 40, 50, 60];
arr.pop(); // returns 60
console.log( arr ); // [10, 20, 30, 40, 50]

Remove element from the beginning of an array.

Array shift - Remove and returns an element from the beginning of an array

var arr = [10, 20, 30, 40, 50, 60];
arr.shift(); // returns 10
console.log( arr ); // [20, 30, 40, 50, 60]

Remove element from any position of an array.

Array splice - Removes an element from any specified position of an array.

var arr = [10, 20, 30, 40, 50];
var removed = arr.splice(3,1);
// the first parameter is the index position where to start
// ther second parameter is the number of elements to be deleted
console.log(arr); // [10, 20, 30, 50]

**Splice can be used to add elements in an array as well.

Delete a specified index element value from array

Array delete - Deletes the specified index element value of an array and assigns undefined to that index. Hence the length of array never changes.

var arr = [10, 20, 30, 40];
delete arr[2]; // delete element with index 2
console.log( arr ); // [10, 20, undefined, 40]
Edit this page on GitHub