Skip to content

Instantly share code, notes, and snippets.

@Magellol
Last active December 14, 2016 16:58
Show Gist options
  • Save Magellol/1a3fdd042cd3c21e59ba9b6e1fea61c3 to your computer and use it in GitHub Desktop.
Save Magellol/1a3fdd042cd3c21e59ba9b6e1fea61c3 to your computer and use it in GitHub Desktop.
Array.concat() or the spread syntax creates shallow copies.
const original = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
];
const clone = [...original];
console.log(original === clone); // false.
console.log(original[0] === clone[0]); // true.
original.splice(0, 2); // clone would not be altered.
original[0].splice(0, 2, 'a'); // clone _will_ be altered because sub-arrays are passed in as reference.
// Same scenario with Array.concat().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment