Skip to content Skip to sidebar Skip to footer

Get The Last Two Positions Of An Array

I would like to know how can I obtain the last two elements of this array like this: array = [1,5,7,8,10,12,23,24]; I am trying to obtain with slice() function but it doesn´t wor

Solution 1:

Use Array#slice with negative index.

var array = [1,5,7,8,10,12,23,24];

console.log(array.slice(-2));

You can learn more about this method also here.

Solution 2:

array = [1,5,7,8,10,12,23,24];


console.log(array.slice(-2))

Use slice -2

Solution 3:

aray.prototype.splice() also

Note:Splice it will update the original array also

var array = [1,5,7,8,10,12,23,24];

console.log(array.splice(-2));

Post a Comment for "Get The Last Two Positions Of An Array"