title | tags |
---|---|
coalesce |
type,beginner |
Returns the first defined, non-null argument.
- Use
Array.prototype.find()
andArray.prototype.includes()
to find the first value that is not equal toundefined
ornull
.
const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));
coalesce(null, undefined, '', NaN, 'Waldo'); // ''