Optional Chaining (?.) in javascript | Modern Javascript | JavaScript Tutorial

Опубликовано: 17 Февраль 2026
на канале: Techies Flock
63
9

Optional Chaining (?.) in javascript | Modern Javascript | JavaScript Tutorial



The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn’t exist.

The optional chaining ?. stops the evaluation if the value before ?. is undefined or null and returns undefined.

The optional chaining ?. is not an operator, but a special syntax construct, that also works with functions and square brackets.


Summary:-
The optional chaining ?. syntax has three forms:
obj?.prop – returns obj.prop if obj exists, otherwise undefined.
obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.
obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.
As we can see, all of them are straightforward and simple to use. The ?. checks the left part for null/undefined and allows the evaluation to proceed if it’s not so.
A chain of ?. allows to safely access nested properties.