โ๐ป Optional chaining is a feature in JavaScript that makes it easier and safer to access properties.It prevents runtime errors that occur when trying to access properties or methods on null or undefined.
๐ Optional chaining (?.) is a feature in JavaScript that simplifies the process of accessing nested properties in objects, especially when those properties may not exist or be undefined or null.
โฆ๏ธ Basic Object Property Access

โฆ๏ธ Handling Undefined Properties

Here the error came and to prevent this error it is made undefine with
optional chaining.
โฆ๏ธ Arrays and Optional Chaining

โฆ๏ธ Handling Function Calls
Advantages of JavaScript Optional Chaining:
i) Prevents Runtime Errors:
Safely access nested properties without throwing errors when
encountering null or undefined.
ii) Cleaner and More Readable Code:
Reduces the need for lengthy if checks or && chaining to verify object
existence.
iii) Optional Function Calls:
Allows safe invocation of functions or methods that may not exist,returning undefined instead of an error.

Top comments (0)