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

8

u/[deleted] Feb 20 '18

But that's stringly typed. Not even really fancy linters can help there

1

u/voyti Feb 20 '18

Sure, there are drawback to this method, although I've been using it in a large project for quite some time now and frankly didn't find many practical issues so far. I'm not saying you're wrong obviously, but can you provide some actuals example of when this could cause issues?

3

u/[deleted] Feb 20 '18

I'm a typescript guy (or any statically typed language) so I view stringly typed as the single worst thing possible. The potential to make typos that are difficult to find is just too high for me.

3

u/voyti Feb 20 '18

Okay, I'm still living in a happy untyped land of whateverness, so less of an issue here probably.