DEV Community

Discussion on: Well splice seems interesting

Collapse
 
joas8211 profile image
Jesse Sivonen

There's better ways to clone and append to an array than splice. Splice actually mutates the original array and I use it mainly to remove an element from an array. Could you explain why you want to use splice? For the sake of experimenting? With ES6 you can initiate a new array and use spread to create such an array.

const originalArray = [1, 2, 3];
const newArray = [...originalArray, 4, 5, 6];
Enter fullscreen mode Exit fullscreen mode
Collapse
 
danielarmbruster0314 profile image
danielarmbruster0314

Yeah I was just trying to better understand it’s functionality and this seemed like a small enough experiment to test the potential implementation. Definitely agree that your example using the spread operator is a much more practical approach.