{"id":321852,"date":"2020-09-24T11:47:44","date_gmt":"2020-09-24T18:47:44","guid":{"rendered":"https:\/\/css-tricks.com\/?p=321852"},"modified":"2020-09-24T11:47:45","modified_gmt":"2020-09-24T18:47:45","slug":"web-technologies-and-syntax","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/web-technologies-and-syntax\/","title":{"rendered":"Web Technologies and Syntax"},"content":{"rendered":"\n
JavaScript has a (newish) feature called optional chaining<\/a>. Say I have code like:<\/p>\n\n\n\n If Now if const name = Data.person.name;<\/code><\/pre>\n\n\n\n
person<\/code> happens to not exist on
Data<\/code>, I’m going to get a hard, show-stopping error. With optional chaining, I can write:<\/p>\n\n\n\n
const name = Data.person?.name;<\/code><\/pre>\n\n\n\n\n\n\n\n
person<\/code> doesn’t exist,
name<\/code> becomes
undefined<\/code> 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. <\/p>\n\n\n\n