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.
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:
All three copies are separate arrays:
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:
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:
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:
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, orArray.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
- How do I make a flat list out of a list of lists?
- How do I merge a list of dicts into a single dict?
- How do I merge multiple lists into one list?
- How do I merge two dictionaries in a single expression in Python?
- How do I merge two lists of tuples based on a key?
- How do I modify the background color of a List in SwiftUI?
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I pass a scalar via a TensorFlow feed dictionary

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 courseTrack 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.