Bird
0
0

Identify the error in this Angular template snippet:

medium📝 Debug Q14 of 15
Angular - Templates and Data Binding
Identify the error in this Angular template snippet:
{{ user?.address.city }}
where user can be null or have address as null.
ANo error, safe navigation is used correctly
BError because safe navigation operator cannot be chained
CError because <code>city</code> is accessed without safe navigation
DError because <code>user</code> is not checked for null
Step-by-Step Solution
Solution:
  1. Step 1: Analyze safe navigation usage

    The snippet uses user?.address.city. The safe navigation is only before address, not before city.
  2. Step 2: Identify potential null access

    If user is not null but address is null, accessing city without safe navigation causes an error.
  3. Final Answer:

    Error because city is accessed without safe navigation -> Option C
  4. Quick Check:

    Chain safe navigation for nested nulls [OK]
Quick Trick: Use ?. before each nested property to avoid errors [OK]
Common Mistakes:
  • Assuming one ?. covers all nested properties
  • Ignoring null possibility of nested objects
  • Thinking chaining ?. is invalid syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes