Junior JavaScript

What is the difference between shallow copy and deep copy?

Type Description

Shallow

Copy

Copies top-level properties only

Deep Copy Copies all nested objects too

Follow me on LinkedIn:

Example:

const obj = { a: { b: 1 } };

const shallow = { ...obj }; // same reference

const deep = JSON.parse(JSON.stringify(obj)); // new copy

More from JavaScript Tutorial

All questions for this course