Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Created May 31, 2021 14:25
Show Gist options
  • Save themgoncalves/17b1aa6efa0e1f489aa081e90bea34f1 to your computer and use it in GitHub Desktop.
Save themgoncalves/17b1aa6efa0e1f489aa081e90bea34f1 to your computer and use it in GitHub Desktop.
JavaScript - example of data structure mutation
/**--------------------- ARRAY ---------------------*/
const species = ['octopus', 'squid', 'shark',];
console.log('initial state', species);
// outputs: 'octopus', 'squid', 'shark'
// 'push' method mutates the array
// by adding a new item to its set
species.push('seahorse');
console.log('mutated state', species);
// outputs: 'octopus', 'squid', 'shark', 'seahorse'
/**--------------------- OBJECT ---------------------*/
const person = { name: 'John Doe', };
console.log('initial state', person.name);
// outputs: 'John Doe'
// overwrites the initial property value
person.name = 'Janie Doe';
console.log('mutated state', person.name);
// outputs: 'Janie Doe'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment