# \[syntax\] chainable nullish coalescing operator aka Nullish Coalescing Ternary

**URL:** <https://es.discourse.group/t/syntax-chainable-nullish-coalescing-operator-aka-nullish-coalescing-ternary/498>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 8, 2020, 9:18am UTC](https://es.discourse.group/t/syntax-chainable-nullish-coalescing-operator-aka-nullish-coalescing-ternary/498 "2020-10-08T09:18:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TineyseHefuron](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@TineyseHefuron](https://es.discourse.group/u/TineyseHefuron)\
**Post date:** [October 8, 2020, 9:18am UTC](https://es.discourse.group/t/syntax-chainable-nullish-coalescing-operator-aka-nullish-coalescing-ternary/498/1 "2020-10-08T09:18:33Z")

</div>

When writing deep data structure we generally need to do something like

```javascript
cache[server] ??= {}
cache[server][channel] ??= {}
cache[server][channel][service] = options

```

This is generally really verbose and we can already compact this by putting each part in parenthese :

```javascript
((cache[server] ??= {})[channel] ??= {})[service] = options

```

but for each level of null coalescing we need to put the parentheses, so I propose to create the nullish coalescing ternary which don't need parentheses

```javascript
cache[server]??{}:[channel]??{}:[service]=options

```

In this case adding `{}` or `[]` just after ?? would permit to indicate what's expected to assign if nullish, but we could apply anything we want.

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [May 31, 2021, 11:42pm UTC](https://es.discourse.group/t/syntax-chainable-nullish-coalescing-operator-aka-nullish-coalescing-ternary/498/2 "2021-05-31T23:42:12Z")

</div>

No offence, but that last line is super unreadable :D  
I too wanted this in more than one language. Perhaps even before I learned that Perl does this automagically, without special syntax. In languages without a concise way to do it, I always end up with a function like this:

```javascript
function autovivify(obj, ...keys) {
  let o = obj
  for (const k of keys) {
    o = o[k] ??= {}
  }
  return o
}

autovivify(cache, server, channel)[service] = options

```

I don't see why you would want to create anything other than an object in the middle of the sequence, and allowing arbitrary expressions there would just lead to even less readable mess. I think that if autovivification were to be supported, it'd better be limited, covering only the most obvious cases (like the one you described), with minimal amount of clutter. Such as a yelling assignment operator:

```javascript
cache[server][channel][service] !!= options
// equivalent to the autovivify example above

```
