How to duplicate/clone array in JavaScript in two lines of code

JavaScript has many possible ways how to clone an array. But there are the most common and straightforward approaches which I’ll go over in this article.

This tutorial shows you four popular ways:

  1. Spread Operator
  2. Array.slice method
  3. Array.concat method
  4. Array.from

Spread operator

Since ES6 has released, this has been the most popular and easiest method to duplicate an array.

Use spread operator to create a clone of your array by “spreading” the item of the original array into a clone:

Array.slice method

The second approach is using slice() method.

Just calling .slice() on the original array to clone the array.

Array.concat method

You could also use .concat() method for cloning an array. Array.concat combines array with values or other arrays.

Array.from method

There is another (but not the last one indeed) method that could simply duplicate a given array.

Take a note that all given examples in this article allows to create only shallow copy. As we remember Arrays/Objects in JavaScript values are copied by reference instead by value.

How to deep clone an array in JavaScript we will learn in one of next articles.



I am a true fan of web development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store