Files
30-seconds-of-code/snippets/coalesce.md
2020-03-05 23:06:58 +02:00

376 B

title, tags
title tags
coalesce utility,beginner

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'); // ''