Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Created September 14, 2019 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaGarcia/66d49dc57cfcc7f095df0a15e28388ad to your computer and use it in GitHub Desktop.
Save NyaGarcia/66d49dc57cfcc7f095df0a15e28388ad to your computer and use it in GitHub Desktop.
Copying an object immutably with the spread operator
const mySquirtle = {
name: 'Squirtle',
type: 'Water',
hp: 100
};
const anotherSquirtle = { ...mySquirtle };
anotherSquirtle.hp = 0;
console.log(anotherSquirtle); //Result: { name: 'Squirtle', type: 'Water', hp: 0 }
console.log(mySquirtle); //Result: { name: 'Squirtle', type: 'Water', hp: 100 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment