Files
30-seconds-of-code/snippets/coalesce.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

473 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Argument coalescing type beginner 2017-12-17T10:08:55+02:00 2020-09-15T16:28:04+03:00

Returns the first defined, non-null argument.

  • Use Array.prototype.find() and Array.prototype.includes() to find the first value that is not equal to undefined or null.
const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));
coalesce(null, undefined, '', NaN, 'Waldo'); // ''