arr.copyWithin(target, start[, end = this.length])
The function takes a section of the array and copies it to another place within the array.
target: index where to copy the elements
start: beginning of section to copy
end: end of section to copy. Optional, if not specified the section will go to the end of the array
[0,1,2,3,4,5].copyWithin(4, 1, 3)
// Result: [0, 1, 2, 3, 1, 2]
Here is a visual representation of what happens:
- As expected parameters are 0 based indexes.
copyWithinmodifies the array itself, it does not return a copy of it.
- The array size if not modified,
copyWithinonly copies what fits.