DEV Community

Cover image for How to use the ?. operator in Javascript

How to use the ?. operator in Javascript

Arika O on June 19, 2020

Because in Javascript objects can be very complex and have unpredictable structures, sometimes is hard to access their properties. There's a real p...
Collapse
 
salyadav profile image
Saloni Yadav

As far as I know, this is available even in ES2015, is it not?

Collapse
 
harken24 profile image
Jozo

It's not, it's part of the ES2020 :)
freecodecamp.org/news/javascript-n...

Collapse
 
salyadav profile image
Saloni Yadav

developer.mozilla.org/en-US/docs/W...

I saw it here and since it’s documented here, assumed it’s already present. I see the document was updated only recently and the cross browser stability has still not kicked in.
However I was able to use this in Typescript with es6 build (babel with backward compatibility) atleast 6-8 months back. Queer!

Thread Thread
 
kenbellows profile image
Ken Bellows • Edited

Usually when I read "ES6 build" it means it transpiles to ES6 code, rather than that it is already ES6 compatible. This operator, fortunately, is super easy to transpile back to older syntax, so Typescript has supported it (and the nullish coalescing operator) for a little while now

(Edit for tone, and to add some details)

Thread Thread
 
salyadav profile image
Saloni Yadav

This puts a lot of things into perspective! :) Thank you!

Collapse
 
arikaturika profile image
Arika O • Edited

To be honest, it seemed familiar to me too but now I realize I must know it from Typescript.

Collapse
 
salyadav profile image
Saloni Yadav

Right yes! This must have been typescript and not JS :) anyways, a good list!

Collapse
 
pozelim profile image
Pedro Ozelim

Nice feature and post!

Collapse
 
arikaturika profile image
Arika O

Thx :)!

Collapse
 
terkwood profile image
Felix Terkhorn

Nice to see this in JS! ☕

Collapse
 
detzam profile image
webstuff

This.. this can becone usefull, tanks

Collapse
 
arikaturika profile image
Arika O

Hope it helped :).