17 lines
375 B
Markdown
17 lines
375 B
Markdown
---
|
|
title: coalesce
|
|
tags: type,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`.
|
|
|
|
```js
|
|
const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));
|
|
```
|
|
|
|
```js
|
|
coalesce(null, undefined, '', NaN, 'Waldo'); // ''
|
|
```
|