Update coalesce description
This commit is contained in:
@ -3,14 +3,14 @@ title: coalesce
|
|||||||
tags: utility,beginner
|
tags: utility,beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
Returns the first non-null/undefined argument.
|
Returns the first defined, non-null argument.
|
||||||
|
|
||||||
Use `Array.prototype.find()` to return the first non `null`/`undefined` argument.
|
Use `Array.prototype.find()` and `Array.prototype.includes()` to find the first value that is not equal to `undefined` or `null`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
|
const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
coalesce(null, undefined, '', NaN, 'Waldo'); // ""
|
coalesce(null, undefined, '', NaN, 'Waldo'); // ''
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user