Shallow Copy VS Deep Copy
Nov 23, 2022
Recently I was asked about copying JavaScript objects. It made me realize I didn't deeply understand some of the core concepts of this fundamental JavaScript topic.
Since nearly everything in JavaScript is an object except for the primitives (boolean, null, undefined, string, number and symbol), we can say that objects are the foundation of JavaScript and permeate its every aspect.
Although copying seems straightforward, a lot of things can go wrong if you use a naive approach. In fact there are two different ways, known as shallow copy and deep copy.
I want to discuss them in this article but before starting, a key concept to understand is mutability.