Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DoctorDerek/4b3f092b6fdc9ce3143b9bafaadba752 to your computer and use it in GitHub Desktop.
Save DoctorDerek/4b3f092b6fdc9ce3143b9bafaadba752 to your computer and use it in GitHub Desktop.
Not Safe to Copy: Date, undefined, and Infinity https://medium.com/p/c3f2783661e9
const notSafeToCopy = [new Date(), undefined, [Infinity]]
const copy = JSON.parse(JSON.stringify(notSafeToCopy))
console.info(notSafeToCopy) // [Date object, undefined, [Infinity]]
console.info(copy) // ["2022-04-24T20:09:16.309Z", null, [null]]
// At least we did make a deep copy, but the data is all wrong:
const copyDos = JSON.parse(JSON.stringify(notSafeToCopy))
copyDos[2][0] = -Infinity // Change deeply-nested [null] to [-Infinity]
console.info(notSafeToCopy) // [Date object, undefined, [Infinity]]
console.info(copyDos) // ["2022-04-24T20:09:16.309Z", null, [-Infinity]]
// The change to the "deep copy" didn't affect the original array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment