Consider the following example:
The example given above,
throws an error because
user2 does not have
the property "full-name".
Optional chaining ?. can also
be used to access a property
of an object using the bracket notation.
In the example given above:
-
user1?.["full-name"] accesses the
"full-name" property of user1
only if user1 is not null or undefined.
Since, user1 is not null or undefined,
it is displayed.
-
user2?.["full-name"] accesses the
"full-name" property of user2
only if user2 is not null or undefined.
Since, user2 is null,
undefined is displayed.