Files
30-seconds-of-code/snippets/coalesce.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

19 lines
451 B
Markdown

---
title: coalesce
tags: type,beginner
firstSeen: 2017-12-17T10:08:55+02:00
lastUpdated: 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`.
```js
const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));
```
```js
coalesce(null, undefined, '', NaN, 'Waldo'); // ''
```