Skip to content

Instantly share code, notes, and snippets.

@bookcasey
Created November 16, 2022 19:21
Show Gist options
  • Save bookcasey/1b0605f3a3d6c18f9f63b7515758f94b to your computer and use it in GitHub Desktop.
Save bookcasey/1b0605f3a3d6c18f9f63b7515758f94b to your computer and use it in GitHub Desktop.
// Mutating data
const favoriteNumbers = [1,2,3];
// favoriteNumbers.push(4); // Mutated favoriteNumbers
const newFavoriteNumbers = [...favoriteNumbers, 4]; // No mutation here!
const removedOne = favoriteNumbers.slice(1, 2); // Mutates the array, no good
console.log('removedOne', removedOne)
console.log('favoriteNumbers', favoriteNumbers);
let a = 10;
// a = 20; // Mutate `a`
// count++; // Mutated count
const b = a + 10; // NOT mutating a
// console.log(a);
// console.log(b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment