Skip to content

Instantly share code, notes, and snippets.

@nicc777
Created September 8, 2019 11:53
Show Gist options
  • Save nicc777/ed7d98d1952688be6307d881b5312747 to your computer and use it in GitHub Desktop.
Save nicc777/ed7d98d1952688be6307d881b5312747 to your computer and use it in GitHub Desktop.
JavaScript - Object reference vs copy
const person = {
name: 'Pete'
};
const newPerson1 = person; // person is referenced (not a copy)
const newPerson2 = {
...person // perons is spread (copied) and the reference is not used
};
person.name = 'Jan';
console.log(person); // Jan
console.log(newPerson1); // Jan - because newPerson1 uses a pointer to person
console.log(newPerson2); // Pete - because the original value was copied into newPerson2
@nicc777
Copy link
Author

nicc777 commented Sep 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment