Javascript - .slice() and .splice()
Code Life
By Pierre Marsaa
Taking a Piece of an Array
Imagine you have a squad of four Codémon in your backpack, but a tournament battle only allows you to send in your first two Codémon. You want to extract those two into a new array without modifying your full team.
The .slice(start, end) method copies a portion of an array and returns it as a brand-new array:
↗️
Loading editor...
↶
↷
Notice two important rules:
- The start index (0) is included, while the end index (2) is excluded (it stops right before index 2, so it grabs indexes 0 and 1).
- The original team array is never modified.
Verify