JavaScript version
coalesce.js
const coalesce = (...args) => args.find((item) => item !== undefined && item !== null);
// Or
const coalesce = (...args) => args.find((item) => ![undefined, null].includes(item));
TypeScript version
coalesce.ts
const coalesce = (...args: any[]): any[] => args.find((item) => item !== undefined && item !== null);
// Or
const coalesce = (...args: any[]): any[] => args.find((item) => ![undefined, null].includes(item));
Examples
examples.js
coalesce(undefined, null, 'helloworld', NaN); // 'helloworld'