Remove last item from array

Prithviraj Mitra picture Prithviraj Mitra · Oct 23, 2013 · Viewed 638.1k times · Source

I have the following array.

var arr = [1,0,2];

I would like to remove the last element i.e. 2.

I used arr.slice(-1); but it doesn't remove the value.

Answer

Stuart Kershaw picture Stuart Kershaw · Oct 23, 2013

Array.prototype.pop() by JavaScript convention.

let fruit = ['apple', 'orange', 'banana', 'tomato'];
let popped = fruit.pop();

console.log(popped); // "tomato"
console.log(fruit); // ["apple", "orange", "banana"]