How to easily truncate an array with JavaScript?

There is a slice method

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr = arr.slice(0, 4);
console.log(arr);

Will return the first four elements.

Don’t forget to assign it back to your variable if you want to discard the other values.

Note: This is just regular javascript, no need for jquery.

Leave a Comment