Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile

Keybase proof

I hereby claim:

  • I am nyagarcia on github.
  • I am nyachan (https://keybase.io/nyachan) on keybase.
  • I have a public key ASCX_KYHXdj9trZ43Hq4aShjNWxZxVQ-XNDJM8IMc9sV1go

To claim this, I am signing this object:

@NyaGarcia
NyaGarcia / object-mutation.js
Last active December 20, 2021 13:43
An example of object mutation in JS
const mySquirtle = {
name: 'Squirtle',
type: 'Water',
hp: 100
};
const anotherSquirtle = mySquirtle;
anotherSquirtle.hp = 0;
console.log(mySquirtle); //Result: { name: 'Squirtle', type: 'Water', hp: 0 }
@NyaGarcia
NyaGarcia / immutable-object.js
Created September 14, 2019 15:18
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 }
@NyaGarcia
NyaGarcia / immutable-array.js
Created September 14, 2019 15:27
Copying an array immutably with the spread operator
const pokemon = ['Squirtle', 'Bulbasur', 'Charmander'];
const pokedex = [...pokemon];
pokedex.push('Cyndaquil');
console.log(pokemon); //[ 'Squirtle', 'Bulbasur', 'Charmander' ]
console.log(pokedex); //[ 'Squirtle', 'Bulbasur', 'Charmander', 'Cyndaquil' ]
@NyaGarcia
NyaGarcia / merge-objects.js
Created September 14, 2019 15:58
Merging objects with the spread operator
const baseSquirtle = {
name: 'Squirtle',
type: 'Water'
};
const squirtleDetails = {
species: 'Tiny Turtle Pokemon',
evolution: 'Wartortle'
};
@NyaGarcia
NyaGarcia / nodeList-to-array.js
Created September 14, 2019 16:25
Converting a nodeList to array with the spread operator
const nodeList = document.getElementsByClassName("pokemon");
const array = [...nodeList];
console.log(nodeList); //Result: HTMLCollection [ div.pokemon, div.pokemon ]
console.log(array); //Result: Array [ div.pokemon, div.pokemon ]
@NyaGarcia
NyaGarcia / nested-object-mutation.js
Last active December 20, 2021 13:52
Showing an example of mutation of an array within an object cloned with spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water',
abilities: ['Torrent', 'Rain Dish']
};
const squirtleClone = { ...pokemon };
pokemon.name = 'Charmander';
pokemon.abilities.push('Surf');
@NyaGarcia
NyaGarcia / math-max-by-index.js
Created September 16, 2019 15:57
Passing array elements by index to Math.max function
const numbers = [1, 4, 5];
const max = Math.max(numbers[0], numbers[1], numbers[2]);
console.log(max); //Result: 5
@NyaGarcia
NyaGarcia / math-max-spread.js
Created September 16, 2019 15:59
Passing parameters to Math.max with spread operator
const numbers = [1, 4, 5, 6, 9, 2, 3, 4, 5, 6];
const max = Math.max(...numbers);
console.log(max); //Result: 9
@NyaGarcia
NyaGarcia / destructure-rename.js
Created September 21, 2019 14:43
Renaming object properties while destructuring them
const squirtle = { type: 'Water', ability: 'Torrent' };
//Normal property destructuring:
const { type, ability } = squirtle;
console.log({ type }, { ability }); //Result: { type: 'water' } { ability: 'torrent' }
//Renaming properties
const { type: squirtleType, ability: superAwesomeAbility } = squirtle;
console.log({ squirtleType }, { superAwesomeAbility }); //Result: { squirtleType: 'Water' } { superAwesomeAbility : 'Torrent' }