Recall & Review
beginner
What is optional chaining in TypeScript?
Optional chaining is a syntax that allows you to safely access nested object properties without causing an error if a property is null or undefined. It uses the
?. operator.Click to reveal answer
beginner
How does optional chaining help with types in TypeScript?
It helps by preventing runtime errors when accessing properties that might not exist, and TypeScript understands this to allow safer code with better type checking.
Click to reveal answer
beginner
What will this expression return if
user is null? <br>user?.nameIt will return
undefined instead of throwing an error because optional chaining stops the access if user is null or undefined.Click to reveal answer
intermediate
Can optional chaining be used with function calls? Give an example.
Yes. For example,
obj?.method() calls method only if obj is not null or undefined. Otherwise, it returns undefined.Click to reveal answer
intermediate
What happens if you use optional chaining on an array element like
arr?.[0]?It safely accesses the first element of
arr only if arr is not null or undefined. If arr is null or undefined, it returns undefined without error.Click to reveal answer
What does the optional chaining operator
?. do in TypeScript?✗ Incorrect
Optional chaining stops accessing further properties if the value is null or undefined and returns undefined instead of throwing an error.
Which of these is a valid use of optional chaining?
✗ Incorrect
Optional chaining must be used before the property you want to safely access, so
user?.address?.street is correct.What will
obj?.method() do if obj is undefined?✗ Incorrect
Optional chaining prevents the call if
obj is undefined and returns undefined safely.Can optional chaining be used to safely access array elements?
✗ Incorrect
Optional chaining works with arrays using bracket notation, e.g.,
arr?.[0].If
user is null, what does user?.name ?? 'Guest' return?✗ Incorrect
Optional chaining returns undefined for
user?.name if user is null, then the nullish coalescing operator ?? returns 'Guest'.Explain how optional chaining improves safety when accessing nested properties in TypeScript.
Think about what happens if a property is missing or null.
You got /4 concepts.
Describe the difference between using optional chaining and normal property access in TypeScript.
Consider what happens when you try to access a property on null or undefined.
You got /4 concepts.