Skip to content

Commit

Permalink
[guide] add nullish-coalescing-operator definition
Browse files Browse the repository at this point in the history
  • Loading branch information
khadijagardezi authored and ljharb committed Jul 2, 2023
1 parent 7916d6f commit fd77bbe
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,33 @@ Other Style Guides
const bar = a + (b / c) * d;
```
<a name="nullish-coalescing-operator"></a>
- [15.9](#nullish-coalescing-operator) The nullish coalescing operator (`??`) is a logical operator that returns its right-hand side operand when its left-hand side operand is `null` or `undefined`. Otherwise, it returns the left-hand side operand.
> Why? It provides precision by distinguishing null/undefined from other falsy values, enhancing code clarity and predictability.
```javascript
// bad
const value = 0 ?? 'default';
// returns 0, not 'default'

// bad
const value = '' ?? 'default';
// returns '', not 'default'

// good
const value = null ?? 'default';
// returns 'default'

// good
const user = {
name: 'John',
age: null
};
const age = user.age ?? 18;
// returns 18
```
**[⬆ back to top](#table-of-contents)**
## Blocks
Expand Down

0 comments on commit fd77bbe

Please sign in to comment.