# optional nullish destructuring defaults

**URL:** <https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427>\
**Category:** 💡 Ideas\
**Created:** [August 17, 2022, 4:17pm UTC](https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427 "2022-08-17T16:17:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![runfaj](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/runfaj/32/1471_2.png) [@runfaj](https://es.discourse.group/u/runfaj)\
**Post date:** [August 17, 2022, 4:17pm UTC](https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427/1 "2022-08-17T16:17:13Z")

</div>

I couldn't seem to find this existing topic, but I'm sure it has been thought about thousands of times (I'm seeing stuff online for over a decade).

It would be nice to have some optional/nullish syntax to allow destructuring defaults to include both undefined and null. Currently, we have this which will only populate the default if the value is undefined:

```javascript
const { myVar = 'default' } = otherVar;
// otherVar = {} has myVar = 'default' for output
// otherVar = { myVar: null } has myVar = null for output
// otherVar = { myVar: 1 } has myVar = 1 for output

```

However, it would nice to have a syntax to have null || undefined revert to the default. Something like:

```javascript
const { myVar ?= 'default' } = otherVar;
// otherVar = {} has myVar = 'default' for output
// otherVar = { myVar: null } has myVar ='default' for output
// otherVar = { myVar: 1 } has myVar = 1 for output

```

A couple potential ways to define this:  
`const { myVar ?= 'default' } = otherVar;`  
`const { myVar? = 'default' } = otherVar;`  
`const { myVar ?? 'default' } = otherVar;`

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [August 17, 2022, 6:40pm UTC](https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427/2 "2022-08-17T18:40:33Z")

</div>

Some previous conversation here: [Null-coalescing default values in destructuring](https://esdiscuss.org/topic/null-coalescing-default-values-in-destructuring)

---

<div class="post-metadata">

**Author:** ![runfaj](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/runfaj/32/1471_2.png) [@runfaj](https://es.discourse.group/u/runfaj)\
**Post date:** [August 18, 2022, 4:34pm UTC](https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427/3 "2022-08-18T16:34:21Z")

</div>

I think that original thread makes good points. There's lots of cases where this would be useful, but a very common case is with incoming json from an api or something where null and undefined are treated equally as being empty. We've got the nullish options, but the extra lines of code to handle that seem quite unnecessary if we could have similar syntax during destructuring for defaults.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [August 18, 2022, 6:34pm UTC](https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427/4 "2022-08-18T18:34:40Z")

</div>

Perhaps the syntax should have two question marks to match [Logical nullish assignment (??=) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment)

Related is if this would also work for function arguments [Nullish coalescing syntax for default param values](https://es.discourse.group/t/nullish-coalescing-syntax-for-default-param-values/483)

---

<div class="post-metadata">

**Author:** ![runfaj](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/runfaj/32/1471_2.png) [@runfaj](https://es.discourse.group/u/runfaj)\
**Post date:** [August 18, 2022, 10:25pm UTC](https://es.discourse.group/t/optional-nullish-destructuring-defaults/1427/5 "2022-08-18T22:25:40Z")

</div>

Ah yes, that would be perfect to just make all three cases follow that same `??=` syntax for consistency. I think there will be criticism for having it when there are potential alternatives, but allowing the multiple options to define things (as long as the syntax is consistent) is what is great about ecmascript in general.
