const name = Data.person.name;
person happens to not exist on
Data, I’m going to get a hard, show-stopping error. With optional chaining, I can write:
const name = Data.person?.name;
person doesn’t exist,
undefined instead of throwing an error. That’s quite useful if you ask me. In a way, it makes for more resilient code, since there is less possibility of a script that entirely bombs out. But there are arguments that it actually makes for less resilient code, because instead of fixing the problem at the root level (bad data), you’re putting a band-aid on the problem.
Jim Nielsen makes the connection to optional chaining and
!important is a quick workaround.
Anyone familiar with CSS knows that using
I like that take.
!important has in CSS to some degree. Most chatter I hear about
!important in CSS lately is about how you should use it when you really mean it (not for getting out of a jam).
You can support CSS-Tricks by being an MVP Supporter.