coalesce - 返回第一个非空/未定义的参数

优质
小牛编辑
121浏览
2023-12-01

返回第一个 non-null/undefined 的参数。

使用 Array.find() 返回第一个非 null / undefined 的参数。

const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
coalesce(null, undefined, '', NaN, 'Waldo'); // ""