Files
30-seconds-of-code/snippets/js/s/argument-coalescing.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

479 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Argument coalescing snippet javascript
type
flower-portrait-1 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'); // ''