Problem
function deepCopy(obj) {
  return JSON.parse(JSON.stringify(obj));
}

const user = {
  name: 'Tyler',
  age: 32,
  created: new Date(),
}

const copiedUser = deepCopy(user)
Solution
function deepCopy(obj) {
  return JSON.parse(JSON.stringify(obj))
}

const user = {
  name: 'Tyler',
  age: 32,
  created: new Date()
}

const copiedUser = deepCopy(user)

A deep copy of an object is a copy whose properties do not share the same references as those of the source object from which the copy was made. One approach for deep copying in JavaScript is using JSON.stringify and JSON.parse.

This kind of works in our case, but it will convert created to a string. To prevent that, you can use window.structuredClone instead (assuming browser support).

function deepCopy(obj) {
  return window.structuredClone(obj)
}