HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

Remove last item from array

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arraylastfromremoveitem

Problem

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 );

Code Snippets

array.splice(-1)

Context

Stack Overflow Q#19544452, score: 762

Revisions (0)

No revisions yet.