array duplication
copy array
cloning arrays
array methods
programming basics

How do I make a exact duplicate copy of an array?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Making an "exact duplicate" of an array sounds simple, but the real question is whether you need a shallow copy or a deep copy. If the array contains only primitive values, a shallow copy is enough. If it contains nested arrays or objects, you need to decide whether those nested values should also be duplicated.

For a normal JavaScript array, use a shallow copy

If the array contains primitives such as numbers or strings, these common patterns all create an independent top-level copy:

javascript
1const original = [1, 2, 3, 4];
2
3const copy1 = original.slice();
4const copy2 = [...original];
5const copy3 = Array.from(original);
6
7console.log(copy1, copy2, copy3);

All three copies are separate arrays:

javascript
copy1.push(5);
console.log(original); // [1, 2, 3, 4]
console.log(copy1);    // [1, 2, 3, 4, 5]

For arrays of primitive values, this is usually the exact duplicate people want.

Understand what a shallow copy means

A shallow copy duplicates the outer array, but not nested objects inside it.

Example:

javascript
1const original = [{ id: 1 }, { id: 2 }];
2const copy = [...original];
3
4copy[0].id = 99;
5
6console.log(original[0].id); // 99
7console.log(copy[0].id);     // 99

Why? Because both arrays still point to the same inner objects. The top-level container is duplicated, but the nested references are shared.

This is the most common source of confusion when someone says "I copied the array but changing it still changes the original."

Use a deep copy when nested data must be independent

If you need a fully separate nested structure, use a deep copy strategy. In modern JavaScript, the best built-in option is usually structuredClone:

javascript
1const original = [{ id: 1 }, { id: 2 }];
2const copy = structuredClone(original);
3
4copy[0].id = 99;
5
6console.log(original[0].id); // 1
7console.log(copy[0].id);     // 99

This duplicates the nested objects too, so the copied array can change independently.

If structuredClone is unavailable in your environment, you may need a custom deep-copy function or a library-based solution depending on the data types involved.

Do not rely blindly on JSON for deep copies

You will often see this pattern:

javascript
const copy = JSON.parse(JSON.stringify(original));

It can work for very simple JSON-safe data, but it has important limitations:

  • it drops functions
  • it breaks Date, Map, Set, and many custom types
  • it fails on circular references

So while it is a useful trick for simple demo data, it is not a universal "exact duplicate" tool.

Pick the copy type based on your data

Use a shallow copy when:

  • the array contains only primitives
  • nested objects may safely remain shared
  • you only need to change the top-level array structure

Use a deep copy when:

  • the array contains nested objects or arrays
  • changes to nested data must not affect the original
  • you need a truly independent object graph

That distinction matters more than the particular syntax you choose.

Common Pitfalls

The biggest mistake is assuming spread syntax or slice() creates a deep copy. They create only a shallow copy.

Another common issue is using JSON serialization as a universal clone method. It works only for simple JSON-compatible data.

People also talk about an "exact duplicate" without defining whether nested references should remain shared. That ambiguity is the whole problem.

Finally, copying a very large structure deeply can be expensive. Choose the level of duplication that your use case actually needs.

Summary

  • Use slice(), spread syntax, or Array.from() for a shallow copy of a JavaScript array.
  • A shallow copy duplicates the outer array but keeps nested references shared.
  • Use structuredClone() when you need a deep copy of nested data.
  • Avoid assuming JSON.parse(JSON.stringify(...)) is a perfect clone for all data types.
  • The right answer depends on whether your array contains primitives only or nested objects too.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.