TIL adding properties conditionally using the spread syntax
POSTED ON:
TAGS: objects javascript conditionals
You can conditionally add properties to objects!
const givePerks = true;
const wizard = {
health: 20
spells: 'fireball',
... (givePerks && { perks: 'immune to fire' }),
}
if givePerks === false
then the other object never gets added.
Related TILs
Tagged: objects