Back to Browse

JavaScript Tips — Slicing arrays using negative indices

1.1K views
Mar 1, 2023
5:53

Need to remove the last few elements from an array? Try using negative indices with the Array.slice method When you pass in a negative number for the start or end argument to slice, the index is computed by counting backwards from the end of the array. arr.slice(-3) for example starts the slice 3 elements back from the end of the array. You can also use negative indices for the end argument to slice. arr.slice(0, -1) for example will slice from the start of the array (0) to 1 element before the end of the array (-1) Whenever you see a negative number in a slice call, just remember that it means the index is determined by going backwards that many elements from the end of the array #javascript #programming #coding

Download

1 formats

Video Formats

360pmp46.4 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

JavaScript Tips — Slicing arrays using negative indices | NatokHD