Bird
0
0

What is wrong with this Angular template expression?

medium📝 Debug Q7 of 15
Angular - Templates and Data Binding
What is wrong with this Angular template expression?
{{ user?.name.toUpperCase() }}
assuming user can be null.
AtoUpperCase() is not a valid JavaScript method
BSafe navigation operator (?.) is used incorrectly on user
CCalling toUpperCase() without safe navigation on name causes error if name is null
DExpression is valid and safe
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe navigation operator usage

    The safe navigation operator (?.) prevents errors if user is null or undefined.
  2. Step 2: Identify problem with method call on possibly null property

    Even if user is safe, name could be null, so calling toUpperCase() directly can cause error.
  3. Final Answer:

    Calling toUpperCase() without safe navigation on name causes error if name is null -> Option C
  4. Quick Check:

    Use safe navigation before method calls on nullable properties [OK]
Quick Trick: Use ?. before method calls on nullable properties [OK]
Common Mistakes:
  • Assuming safe navigation on user covers name
  • Ignoring possible null values on nested properties
  • Thinking toUpperCase() is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes