typescript / beginner
Snippet
Optional Chaining
The optional chaining operator (?.) permits reading the value of a property deep within a chain of objects without having to check if each reference is valid.
snippet.ts
1
2
const user = { details: { name: 'Bob' } };const name = user?.details?.name;
Breakdown
1
user?.details
Safely checks if 'user' exists before attempting to access 'details'.
2
?.name
Safely checks if 'details' exists before attempting to access 'name'.