DEV Community

Discussion on: JavaScript Interviews: Create a deep copy of an object

Collapse
pris_stratton profile image
pris stratton

My first thought was wouldn’t I just use Object.assign, is that a valid answer?

Collapse
ninofiliu profile image
Nino Filiu

Object.assign is equivalent to object spread and only copies the top level keys of an object

const a = { b: { c: 10 } }
const clonedA = { ...a }
clonedA.b.c++
a.b.c // 11
Enter fullscreen mode Exit fullscreen mode