Use optional chaining in javascript to write better code #shorts

preview_player
Показать описание
Optional chaining is a useful method you can use in javascript to prevent your application from crashing when certain properties on objects might be undefined.

------------

Рекомендации по теме
Комментарии
Автор

I discovered this while working with Typescript. I didn't even know it was available in JS.

MobiusCoin
Автор

You've should have mentioned that this operator goes awesomely with the nullish coalescing operator for example:
user.hobbies?.join(', ') ?? 'The user is boring and has no hobbies'

matrix
Автор

Crucial note: for Frontend developers who have to think backwards compatibility, abusing optional chaining when you’re still targeting ES3 will make your code bloated due to polyfills.

aldeywahyuputra
Автор

We should have null or empty values instead of being uncertain about whether property exists or not. I'd simply have empty values for hobbies, it will assert that this certain property exists for every user, even though it may be empty for some user, will make it easy to just add something to it in case the user develops a hobby in future.

Amitkumar-dvkk
Автор

Be careful to not abuse the optional chain everywhere, otherwise developers think the End point you are trying to fetch is unreliable

Shuyinz
Автор

This is exactly what I was looking for! Thank you for your tips!

Georglider
Автор

This is such a blessing while working with React…. Its just awesome!

RakeshSahu-crks
Автор

PHP has a similar thing. Its called a "null safe operator". Released in PHP 8

jamesjjt
Автор

Thanks for these vids, I love this format

quintonsmylie
Автор

Finally time to get rid of those warnings "map is not a function" because it doesn't recognize it's an array until complete fetching data

ninhdang
Автор

WOW definitely need to do this for one of my projects. Good info!

c-damocles
Автор

Man these coding shorts are telling me so much

alithat
Автор

Nice that's awesome I knew this feature in dart I didn't know it exists in vanilla js too

yassinesafraoui
Автор

I'd prefer checking null with if and do something about it. This is fine for maybe only displaying an object in html but I see this a lot from inexperienced developers on things that would require some calculations or more complex logic involved with the object fields and one null would lead to undefined behavior.

gordonfreimann
Автор

Wow.. optional chaining is amazing.. 😃 Thanks!

sharathkumarhk
Автор

..I would be a bit concerned about not handling the case where property is undefined. I feel there needs to be some if this then x else y kind of logic. This is good for initial deployment if you are on a tight deadline, but feels a bit like masking tape.

That said it would be fantastic for null checks!

tomassullivan
Автор

What's even better is how you can do it to for functions as well foo?.()
Pretty unique to JS compared to other languages that have this concept.

EddieVillamor
Автор

yeah, and function as well: func?.()
😁

hivantm
Автор

Can we use try and catch ? That could be useful too right ?

Bouderbala
Автор

Now, instead of a crash, you get a silent error that will bite you in your back eventually.

rafagd