How to Clone an Array in JavaScript

Making shallow and deep copies of arrays in JavaScript.

There are times when you need to change an array without touching the original. Arrays in JavaScript are stored by reference, so assigning one to a new variable doesn't copy it. Both variables point to the same array:

const fruits = ["apple", "banana", "cherry"]
const moreFruits = fruits
moreFruits.push("mango")
console.log(fruits)
// ["apple", "banana", "cherry", "mango"]

To get a separate array you need to clone it.

TL;DR Use the spread operator [...arr] for a shallow copy and structuredClone(arr) for a deep copy.

Shallow Copies

The spread operator is the most common way to copy an array:

const fruits = ["apple", "banana", "cherry"]
const copy = [...fruits]
copy.push("mango")
console.log(fruits) // ["apple", "banana", "cherry"]
console.log(copy) // ["apple", "banana", "cherry", "mango"]

Array.from and slice do the same thing:

const copyFrom = Array.from(fruits)
const copySlice = fruits.slice()

Calling slice() with no arguments returns everything from the start to the end. Array.from also works on array-like values such as a NodeList or a string. Pick whichever reads best to you. I usually reach for spread.

The Shallow Copy Pitfall

All three of these are shallow copies. The array itself is new, but the items inside it are the same. With strings and numbers that doesn't matter, but objects and nested arrays are still shared references:

const users = [{ name: "Jim" }, { name: "Pam" }]
const copy = [...users]
copy[0].name = "Dwight"
console.log(users[0].name) // "Dwight"

Changing an object in the copy changed the original too. When your array has objects inside it you need a deep copy.

Deep Copies with structuredClone

structuredClone is a new global function that makes a deep copy of a value. It's now supported in all major browsers and in Node 17+.

const users = [{ name: "Jim" }, { name: "Pam" }]
const copy = structuredClone(users)
copy[0].name = "Dwight"
console.log(users[0].name) // "Jim"
console.log(copy[0].name) // "Dwight"

It copies nested objects and arrays all the way down, and it handles Date, Map, Set and even circular references. It can't clone functions or DOM nodes, and will throw a DataCloneError if it finds one.

The Old Way: JSON.parse and JSON.stringify

Before structuredClone the common trick was turning the array into a JSON string and parsing it back:

const copy = JSON.parse(JSON.stringify(users))

This works for plain data, but it has some limits:

  • Date objects turn into strings
  • undefined and functions become null inside arrays (and are dropped from objects)
  • Map and Set become empty objects
  • Circular references throw an error
const items = [new Date("2022-06-02"), undefined, () => {}]
console.log(JSON.parse(JSON.stringify(items)))
// ["2022-06-02T00:00:00.000Z", null, null]

If you still need to support older browsers it's a handy fallback for simple data. Otherwise structuredClone is the better choice.

Use spread for flat arrays of strings and numbers, and structuredClone when the array has objects inside.

Have some feedback on this post? .