DEV Community


Discussion on: JavaScript Equality Checks

ronakjethwa profile image
Ronak Jethwa Author • Edited

Fun Fact: with nested objects, the nested object property is copied by reference as a property to the new Object.

let user = {
  name: "John",
  sizes: {
    height: 182,
    width: 50

let clone = Object.assign({}, user);

console.log(,clone)); //false
console.log(, clone.sizes)); //true

To avoid this, the easiest way in JavaScript to deep clone an object is

let clone2 = JSON.parse(JSON.stringify(user));
console.log(, clone2.sizes)); // false