How to Remove the Last N Elements from an Array in JavaScript?

Estimated read time 1 min read

To remove the last N elements from an array in JavaScript, you can use the splice() method. Here’s an example:

let arr = [1, 2, 3, 4, 5];
let n = 3;
arr.splice(-n, n);
console.log(arr);

This code removes the last N elements from the array [1, 2, 3, 4, 5] using the splice() method and logs the modified array: [1, 2] where N=3.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply