Skip to content

Instantly share code, notes, and snippets.

View Mohamed-Code-309's full-sized avatar
🎯
Focusing

Mohamed Jebril Mohamed-Code-309

🎯
Focusing
View GitHub Profile
@NyaGarcia
NyaGarcia / rest-parameter.js
Created September 23, 2019 15:41
An example showcasing the use of the rest parameter
function printPokemon(name, type, ...abilities) {
console.log(
`${name} is a ${type} type Pokemon. ${name}'s abilities are: ${abilities.join(
', '
)}`
);
}
printPokemon('Squirtle', 'Water', 'Torrent');
//Squirtle is a Water type Pokemon. Squirtle's abilities are: Torrent
@NyaGarcia
NyaGarcia / default-property.js
Last active December 22, 2021 17:38
Adding a default property with the spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water'
};
const { abilities = [], ...rest } = pokemon;
const fullSquirtle = { ...rest, abilities };
console.log(rest); //Result: { name: 'Squirtle', type: 'Water' }
console.log({ fullSquirtle }); //Result: { name: 'Squirtle', type: 'Water', abilities: [] }
@NyaGarcia
NyaGarcia / conditional-property-spread.js
Created September 22, 2019 16:57
Adding properties conditionally with spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water'
};
const abilities = ['Torrent', 'Rain dish'];
const fullPokemon = {
...pokemon,
...(abilities && { abilities })
};
@NyaGarcia
NyaGarcia / short-circuiting-&&.js
Created September 22, 2019 16:48
Short circuiting with && operator
const starterPokemon = ['Squirtle', 'Charmander', 'Bulbasur'];
function choosePokemon(pokemon) {
return pokemon[Math.floor(Math.random() * pokemon.length)];
}
//Checks if the starterPokemon array has any elements and runs the choosePokemon function
console.log(starterPokemon.length > 0 && choosePokemon(starterPokemon));
//It's equivalent to doing this:
if (starterPokemon.length > 0) {
@NyaGarcia
NyaGarcia / conditional-property-if.js
Created September 22, 2019 14:31
Adding properties conditionally with if statement
const pokemon = {
name: 'Squirtle',
type: 'Water'
};
const abilities = ['Torrent', 'Rain dish'];
const fullPokemon = abilities ? { ...pokemon, abilities } : pokemon;
console.log(fullPokemon);
@NyaGarcia
NyaGarcia / destructure-defaults.js
Created September 22, 2019 14:30
Using default values when destructuring
const pokemon = {
id: 1,
name: 'Squirtle'
};
const { type, name } = pokemon;
console.log(name); //Result: Squirtle
console.log(type); //Result: undefined
//Assigning default value to the type variable
@NyaGarcia
NyaGarcia / dynamic-destructure.js
Created September 21, 2019 15:53
Dynamically destructuring object properties with the spread operator
const pokemon = {
id: 1,
name: 'Squirtle',
type: 'Water'
};
//Destructuring a dynamic property
const dynamicProperty = 'name';
const { [dynamicProperty]: value } = pokemon;
@NyaGarcia
NyaGarcia / dynamic-destructure-array-es6.js
Created September 21, 2019 15:40
Dynamically destructuring an array of properties with the spread operator (ES6 Arrow function)
const dynamicDestructureManyES6 = (object, properties) =>
properties.map(property => {
const { [property]: value } = object;
return value;
});
console.log(dynamicDestructureManyES6(pokemon, ['name', 'type'])); //Result: ['Squirtle', 'Water']
@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' }
@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