Search code examples
javascriptarraysslice

Remove last item from array


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.


Solution

  • Use splice(startPosition, deleteCount)

    array.splice(-1)
    

    var array = ['abc','def','ghi','123'];
    var removed = array.splice(-1);  //last item
    console.log( 'array:', array );
    console.log( 'removed:', removed );