This file contains hidden or 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
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 |
This file contains hidden or 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
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: [] } |
This file contains hidden or 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
const pokemon = { | |
name: 'Squirtle', | |
type: 'Water' | |
}; | |
const abilities = ['Torrent', 'Rain dish']; | |
const fullPokemon = { | |
...pokemon, | |
...(abilities && { abilities }) | |
}; |
This file contains hidden or 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
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) { |
This file contains hidden or 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
const pokemon = { | |
name: 'Squirtle', | |
type: 'Water' | |
}; | |
const abilities = ['Torrent', 'Rain dish']; | |
const fullPokemon = abilities ? { ...pokemon, abilities } : pokemon; | |
console.log(fullPokemon); |
This file contains hidden or 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
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 |
This file contains hidden or 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
const pokemon = { | |
id: 1, | |
name: 'Squirtle', | |
type: 'Water' | |
}; | |
//Destructuring a dynamic property | |
const dynamicProperty = 'name'; | |
const { [dynamicProperty]: value } = pokemon; |
This file contains hidden or 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
const dynamicDestructureManyES6 = (object, properties) => | |
properties.map(property => { | |
const { [property]: value } = object; | |
return value; | |
}); | |
console.log(dynamicDestructureManyES6(pokemon, ['name', 'type'])); //Result: ['Squirtle', 'Water'] |
This file contains hidden or 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
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' } |
This file contains hidden or 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
const numbers = [1, 4, 5, 6, 9, 2, 3, 4, 5, 6]; | |
const max = Math.max(...numbers); | |
console.log(max); //Result: 9 |
NewerOlder