# Optional chaining skip nested object next levels

**URL:** <https://es.discourse.group/t/optional-chaining-skip-nested-object-next-levels/1192>\
**Category:** 🦋 Proposals\
**Tags:** proposal\
**Created:** [February 9, 2022, 6:07pm UTC](https://es.discourse.group/t/optional-chaining-skip-nested-object-next-levels/1192 "2022-02-09T18:07:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laukstein](https://avatars.discourse-cdn.com/v4/letter/l/2acd7d/32.png) [@laukstein](https://es.discourse.group/u/laukstein)\
**Post date:** [February 9, 2022, 6:07pm UTC](https://es.discourse.group/t/optional-chaining-skip-nested-object-next-levels/1192/1 "2022-02-09T18:07:30Z")

</div>

I wish optional chaining (?.) have a better design.  
Currently needs `({a: {}}).a?.b?.c?.d?.()` in order not to fall into a code exception.

Instead, if **obj.a.b** doesn't exist, I propose `({a: {}}).a?.b.c.d()` to be enough - skip nested object next levels if found **obj.a.b** as not existing.

ref [https://github.com/tc39/proposals/issues/398](https://github.com/tc39/proposals/issues/398)

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [February 9, 2022, 6:22pm UTC](https://es.discourse.group/t/optional-chaining-skip-nested-object-next-levels/1192/2 "2022-02-09T18:22:38Z")

</div>

That was explicitly rejected during the optional chaining proposal - because it would prevent people who WANT an exception.

The intention is for the `?.` to explicitly mark each place where something might be nullish - it's not to avoid exceptions, it's to simplify the places where you want the short-circuiting behavior. It's optional chaining, not "safe navigation".

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [February 9, 2022, 6:54pm UTC](https://es.discourse.group/t/optional-chaining-skip-nested-object-next-levels/1192/3 "2022-02-09T18:54:30Z")

</div>

In your specific example, you really only need `({a: {}}).a.b?.c.d()` to be safe from exceptions. And I think that's the beauty of how optional chaining is currently designed, it shows exactly where you expect a nullish value to be, and everywhere else you're indicating that you don't expect a nullish to ever show up there.  
I'd also point out that, if a need does come to use `?.` a lot in the same expression, it's not that verbose to do - after all, some languages' property access operator is two characters long by default (`->`).

---

<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:** [February 9, 2022, 7:58pm UTC](https://es.discourse.group/t/optional-chaining-skip-nested-object-next-levels/1192/4 "2022-02-09T19:58:20Z")

</div>

It wouldn’t be quite the same but a `try` expression would be one possible way to have a _safe_ nested access

> [@Try-catch oneliner](https://es.discourse.group/t/try-catch-oneliner/107/):
>
> There is a lot libraries in npm doing similar stuff, if you search [try await](https://www.npmjs.com/search?q=try%20await) you will see a lot libraries which do mostly the same: const [error, result] = tryCatch(JSON.parse, 'hello'); What all this people want is to use more clean syntax for error handling, similar to something [Go already has](https://blog.golang.org/error-handling-and-go). So instead of using such a big construction: let error, data; try { data = JSON.parse('hello'); } catch (e) { error = e; } where you need to use let, 2 code blocks and 3 variables. What I wan…
