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? user?.name
It 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?
AThrows an error if the value is null or undefined
BStops property access if the value is null or undefined and returns undefined
CAlways returns null
DConverts the value to a string
✗ 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?
Auser?.address?.street
Buser.address?.street
Cuser?.address.street
Duser.address.street?.
✗ 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?
ACall method and throw error
BReturn null
CCall method with undefined context
DReturn undefined without calling method
✗ Incorrect
Optional chaining prevents the call if obj is undefined and returns undefined safely.
Can optional chaining be used to safely access array elements?
AYes, like <code>arr?.[0]</code>
BNo, it only works with objects
CYes, but only with numbers
DNo, it causes syntax error
✗ Incorrect
Optional chaining works with arrays using bracket notation, e.g., arr?.[0].
If user is null, what does user?.name ?? 'Guest' return?
AThrows error
Bnull
C'Guest'
Dundefined
✗ 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.
Practice
(1/5)
1. What does the optional chaining operator ?. do in TypeScript?
easy
A. It forces a property to be non-nullable.
B. It throws an error if the property does not exist.
C. It converts a value to a boolean.
D. It safely accesses a property or method, returning undefined if the value is null or undefined.
Solution
Step 1: Understand optional chaining purpose
Optional chaining ?. allows safe access to nested properties or methods without causing runtime errors if the object is null or undefined.
Step 2: Identify behavior on null or undefined
If the value before ?. is null or undefined, it returns undefined instead of throwing an error.
Final Answer:
It safely accesses a property or method, returning undefined if the value is null or undefined. -> Option D
Quick Check:
Optional chaining = safe access without error [OK]
Hint: Optional chaining returns undefined if value is null or undefined [OK]
Common Mistakes:
Thinking it throws an error on missing property
Confusing it with non-null assertion operator
Assuming it converts values to boolean
2. Which of the following is the correct syntax to safely access the property name of an object user using optional chaining?
easy
A. user?.name()
B. user?.name
C. user?.[name]
D. user?.name?
Solution
Step 1: Review optional chaining syntax for properties
The correct syntax to access a property safely is object?.property.
Step 2: Check each option
user?.name uses user?.name which is correct. user?.[name] uses brackets incorrectly. user?.name() treats name as a method call. user?.name? has invalid syntax.
Final Answer:
user?.name -> Option B
Quick Check:
Property access with ?. = user?.name [OK]
Hint: Use ?. before property name without parentheses for safe access [OK]
Common Mistakes:
Adding parentheses when accessing properties
Using brackets incorrectly with optional chaining
Placing ? after property name
3. Consider the following TypeScript code:
type User = { profile?: { age?: number } };
const user: User = {};
const age = user.profile?.age ?? 30;
console.log(age);
What will be the output?
medium
A. 30
B. null
C. Error at runtime
D. undefined
Solution
Step 1: Analyze optional chaining and nullish coalescing
The expression user.profile?.age safely tries to access age. Since profile is undefined, this returns undefined.
Step 2: Apply nullish coalescing operator
The ?? operator returns the right side value 30 because the left side is undefined.
Final Answer:
30 -> Option A
Quick Check:
undefined ?? 30 = 30 [OK]
Hint: ?. returns undefined if missing; ?? provides default value [OK]
Common Mistakes:
Expecting undefined instead of default value
Confusing ?? with || operator
Assuming runtime error on missing property
4. Identify the error in this TypeScript code snippet:
interface Data {
info?: {
value: number;
};
}
const data: Data = {};
const val = data?.info.value;
medium
A. No error, code works fine
B. Runtime error: accessing property of undefined
C. Type error: missing optional chaining before 'value'
D. Syntax error: optional chaining used incorrectly on data
Solution
Step 1: Check optional chaining usage
The code uses data?.info.value. Optional chaining is applied only on data, but info can be undefined.
Step 2: Identify missing optional chaining
Accessing info.value without optional chaining can cause a runtime error if info is undefined. It should be data.info?.value or data?.info?.value.
Final Answer:
Type error: missing optional chaining before 'value' -> Option C
Quick Check:
Optional chaining needed before 'value' to avoid error [OK]
Hint: Chain ?. for each possibly undefined property [OK]
Common Mistakes:
Applying ?. only once for nested properties
Assuming optional chaining on parent covers all children