Created
May 31, 2021 14:25
-
-
Save themgoncalves/17b1aa6efa0e1f489aa081e90bea34f1 to your computer and use it in GitHub Desktop.
JavaScript - example of data structure mutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**--------------------- 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