JavaScript Clone an Array example
Posted on: February 08, 2021 by Deven
If you want to clone an Array in Javascript we can use the spread operator to expand our array into items and clone it into a new array:
consider the following example:
const numbers = [8, 22, 52, 100, 6, 15];
const numbersCopy = [...numbers];
Another better approach is using Array.slice()
method with no arguments. consider the following example.
const numbers = [8, 22, 52, 100, 6, 15];
const numbersCopy = numbers.slice();
Share on social media
//