Anjan Dutta

How to remove last element from array in javascript

How to remove last element from array in javascript

To remove the last element of any array we can use the pop() method in javascript.

The pop() method removes the last element from an array and returns it back.

Remove and return element from the end of an array

Array pop - Removes and returns an 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 without returning the value

This is a trick we can use to remove the last element by decreasing the length of the array. This is a javascript feature that allows us to do such operation.

var arr = [10, 20, 30, 40, 50, 60];
arr.length = arr.length - 1;
console.log( arr ); // [10, 20, 30, 40, 50]

Output

Remove from end of array