r/javascript • • Feb 20 '18

Optional Chaining may be coming to JavaScript

https://dev.to/sammyisa/optional-chaining-may-be-coming-to-javascript--4ff0
321 Upvotes

100 comments sorted by

View all comments

62

u/impreson Feb 20 '18

Thanks for sharing. I like this a lot. It always feels tedious doing the checking, especially in nested objects. Syntactic sugar like this makes programming more fun and lets you focus on the important stuff. This is also one of the reasons why I don’t like php syntax, with its isset, empty, is_null etc.

2

u/voyti Feb 20 '18

I looked for this a lot and it seems currently the best way is to use lodash _.get function, here's an example if anyone's interested:

_.get(parentObject, 'nested.path', optionalDefault)

But nice to see it as native operator. I like to call it 'soft dot' myself

4

u/[deleted] Feb 20 '18

While this is a solution, I refuse to use it because of the difficulties it introduces when refactoring nested keys.