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.


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"]

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

You can do this using .slice() method like:

arr.slice(0, -1);    // returns [1,0]

Here is a demo:

var arr = [1, 0, 2];
var newArr = arr.slice(0, -1);    // returns [1,0]

console.log(newArr);
$('#div1').text('[' + arr + ']');
$('#div2').text('[' + newArr + ']');
<script src="http://code.jquery.com/jquery.min.js"></script>
<b>Original Array    : </b>
<div id="div1"></div>
<br/>
<b>After slice(0, -1): </b>
<div id="div2"></div>

instead of doing :

arr.slice(-1);   // returns [2]

Here is a demo:

var arr = [1, 0, 2];
var newArr = arr.slice(-1);    // returns [2]

console.log(newArr);
$('#div1').text('[' + arr + ']');
$('#div2').text('[' + newArr + ']');
<script src="http://code.jquery.com/jquery.min.js"></script>
<b>Original Array    : </b>
<div id="div1"></div>
<br/>
<b>After slice(-1): </b>
<div id="div2"></div>

Explanation:-

Now the basic syntax of Array.prototype.slice() or in short slice() method is:

arr.slice([begin[, end]])

Here,

the begin parameter is zero-based index at which extraction from an array starts. So, lets say based on above example if we do something like

arr.slice(0)    // returns [1,0,2]

it would return all the array elements from start of sequence from position 0 and that is [1,0,2]. Similarly, if we do

arr.slice(1)    // returns [0,2]

it would return [0,2] since 0 is at position 1 here and everything after that. Now, in your case you have passed a negative index i.e., -1 as the begin parameter, which indicates an offset from the end of the sequence. So, slice(-1) in your case extracts the last one array element in the sequence and that is 2 (as we have already seen in the above demo).

Now, let's talk about the end parameter in the slice() method syntax here. It is again a zero-based index at which extraction from an array ends. So, lets say we have a array like:-

var arr = [1, 0, 2, 5, 3, 9];

and we want to get just the 2,5,3 elements in the array. Now, position of 2 from start of the sequence is 2 and for last element 3 it is 4. We will need to end the extraction here a position 5, as we need to get the element before that position. So, we will simply implement slice() method here like

arr.slice(2, 5)    // returns [2,5,3]

In your case, we have implemented -1 as the end parameter, so our code is like

arr.slice(0, -1)   // returns [1,0]

As a negative index, end indicates an offset from the end of the sequence. So, slice(0,-1) extracts the first element through the second-to-last element in the sequence. So, we get the desired output. We can also do like

arr.slice(0, 2)    // returns [1,0]

we will get the same output. But, I have used -1 here as its easier to implement even for a long array like

[0,2,3,1,2,9,3,6,3,9,1,0,2,9,0,1,1,2,3,4,7,9,1]

If you just want to remove the last element, you don't want to sit & calculate the position of last 9 here and the do like arr.slice(0, 22). You can then simply implement the negative index logic here & do

arr.slice(0, -1) // same result as arr.slice(0, 22)

Hope it helps!


learn by example:

let array_1 = [1,2,3,4];
let array_2 = [1,2,3,4];
let array_3 = [1,2,3,4];
let array_4 = [1,2,3,4];

array_1.splice(-1,1)  // returned --> [4]      array_1 = [1,2,3]
array_2.slice(0,-1);  // returned --> [1,2,3]  array_2 = [1,2,3,4]
array_3.pop();        // returned --> 4        array_3 = [1,2,3]
array_4.shift();      // returned --> 1        array_4 = [2,3,4]