Sometimes while writing the code, we do not know whether the object that we are going to access contains all the necessary properties.
In the example given above,
details property of
This is because the
does not contain the
But when we try to access the
assuming that it is nested inside the
it results in an error.
However, in many practical cases,
undefined is convenient.
In the example given below,
we are using the conditional operator
undefined while accessing a property
that is not stored in the object.
There is no error in the example given above.
But we have to use
This becomes a matter of concern
for deeply nested properties.
Multiple uses of the conditional operator also
make the code difficult to understand.
In such cases, we can use optional chaining to make the code easy to understand and avoid code repetition.