For example, suppose you have an array that is given below:
[235, 585, 985, 36, 452, 231]
Now you want to get the last three items from the array. You can see that the last three items are 36, 452 and 231. So how can we do it programmatically?
Here I am going to give you a simple trick to achieve this task.
All we need to do is just to provide an integer argument which will be the starting point of the array. Well, the argument also can be a negative integer.
See the example below:
var jsArr = [235, 585, 985, 36, 452, 231]; console.log(jsArr.slice(-1)); // Output:  console.log(jsArr.slice(-2)); // Output: [452, 231] console.log(jsArr.slice(-3)); // Output: [36, 452, 231]
So simple and easy. Isn’t it…
Here in our example when we use slice(-1) it is returning the last item from the array. If we are using slice(-3) it is returning the last 3 items from our array.
I hope, you have understood how the array slice method working with a negative integer type parameter and how it is returning the last n items from our array.