Performance: Safe navigation operator for null
This affects rendering speed by preventing unnecessary errors and re-renders caused by null or undefined values in templates.
<div>{{ user?.address?.city }}</div><div>{{ user.address.city }}</div>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct property access without safe navigation | Normal | Triggers extra change detection if errors occur | Normal | [X] Bad |
| Using safe navigation operator (?.) | Normal | Single change detection cycle without errors | Normal | [OK] Good |
?. do in Angular templates??. is used to safely access properties on objects that might be null or undefined, avoiding runtime errors.?., accessing a property on null or undefined causes an error and breaks the app.name property of a possibly null user object in an Angular template??., used as object?.property to avoid errors if object is null.user?.name, which is correct. Options A, C, and D use invalid or unsafe syntax.user = null;, what will be the output of this Angular template snippet?{{ user?.name || 'Guest' }}user is null, user?.name safely returns undefined without error.undefined || 'Guest' evaluates to 'Guest' because undefined is falsy.{{ user?.address.city }} where user can be null or have address as null.user?.address.city. The safe navigation is only before address, not before city.user is not null but address is null, accessing city without safe navigation causes an error.city is accessed without safe navigation -> Option Cprofile which may be null or have null nested properties. How would you safely display the profile.contact.email in the template, showing 'No Email' if any part is null?profile and contact can be null, use ?. before both contact and email.|| 'No Email' to show fallback if the email is missing.Pass the test and this pattern moves unknown