javascript / beginner
Snippet
Safe Property Access with Optional Chaining
The safe navigation operator (?.) allows you to read the value of a property located deep within a chain of connected objects without checking each reference for null or undefined.
snippet.js
1
const userEmail = this.user?.profile?.email;
angular
Breakdown
1
this.user?
Checks if the user object exists before trying to access the profile.
2
.profile?.email
Continues the safe check for the profile object before accessing the final email property.