How do I copy an array in JavaScript?

To copy or clone an array in JavaScript, you can use the array.slice(), array.concat(), array.from() methods, and the spread operator ("..."). The slice() method is the simplest and fastest way to copy the contents of an existing array (the original array remains unchanged) into a new array without any parameters. The concat() method is intended to merge two arrays into one, but you can also use it to clone an array by calling it method on an empty array and passing the original array as a function parameter. The concat() method creates a new copy of the array like slice() method. The from() method is another way to clone an array; it creates a new shallow-copied array (the method only goes down one level when cloning an array). You can also use the spread operator ("...") to clone an array by "expanding" its elements. The spread operator creates a shallow copy of the original array. If you are working with a multi-dimensional array or an array containing objects, you will need to look for other alternatives. In this JavaScript copy array example, we use the spread operator ("..."). Below you can see more examples of copying JavaScript arrays with a detailed description of each method. Click Execute to run the JavaScript Copy Array Example online and see the result.
How do I copy an array in JavaScript? Execute
let array = ['JavaScript', 'Copy', 'Array', 'Example'];

let newArray = [...array];

console.log(newArray);
Updated: Viewed: 794 times

How to clone JavaScript array using the spread operator?

You can use the spread operator ("...") to clone an array in JavaScript. The spread operator ("...") is available in JavaScript ES6 and above and can be used to copy an array by "expanding" its elements:

JavaScript Copy Array with Spread Operator Example
let array = ['JavaScript', 'Clone', 'Array', 'Example'];

console.log(newArray = [...array]);

// output: ['JavaScript', 'Clone', 'Array', 'Example']

How to copy JavaScript array using the slice() method?

The array.slice() method in JavaScript is used to get the part of an array from the start index to the end index. The slice() method does not modify the original array. If the original array contains objects that refer to other objects, only their references will be copied to the new array (shallow copy).

JavaScript Copy Array with slice() method Example
let array = ['JavaScript', 'Copy', 'Array', 'Example'];

console.log(array.slice());

// output: ['JavaScript', 'Copy', 'Array', 'Example']

How to clone JavaScript array using the concat() method?

The array.concat() method in JavaScript is used to concatenate two or more arrays into one. You can also use concat() method to clone an array. The concat() method returns a new array without changing the original one.

JavaScript Clone Array with concat() method Example
let array = ['JavaScript', 'Clone', 'Array', 'Example'];

let newArray = [].concat(array);

console.log(newArray);

// output: ['JavaScript', 'Clone', 'Array', 'Example']

How to clone JavaScript array with the from() method?

The array.from() method in JavaScript creates a new array from the original array. This method was introduced in ES6 and only works in modern browsers, but you can use the polyfill library to get modern functionality in older browsers.

JavaScript Clone Array with from() method Example
let array = ['JavaScript', 'Clone', 'Array', 'Example'];

let newArray = Array.from(array);

console.log(newArray);

// output: ['JavaScript', 'Clone', 'Array', 'Example']

See also