Optional chaining ?. lets you read a property, call a method or index an array without first checking that each step exists. If the value before ?. is null or undefined, the whole expression stops and returns undefined instead of throwing a TypeError. It is part of ES2020 and works in all current browsers and Node.js, which makes "optional chaining javascript" one of the most useful modern syntax features for nested data.
This guide shows the three forms, how short-circuiting works, how it pairs with ??, and where it should not be used.
?.?.()?.[]?? for defaultsData from APIs and configuration files often has missing parts. Reading through a missing part throws.
The old fix is a chain of checks with &&.
Optional chaining says the same thing in one short expression.
?.Put ?. right before the property that might not be reachable. If the left side is null or undefined, the result is undefined.
Only the values directly before each ?. are tested. Here, order itself is assumed to exist. If order could be missing, write order?.customer.
?.()Use ?.() to call a function only when it exists. This is useful for optional callbacks.
The missing onError is skipped silently. Be careful: ?.() only checks for null or undefined. If the property exists but is not a function, such as a string, you still get a TypeError.
?.[]Use ?.[ for bracket access, for example with an index or a computed key.
When the check fails, JavaScript skips the rest of the chain, including function calls and arguments.
The short-circuit covers the whole chain that follows, not only the next property. In a?.b.c.d, if a is nullish, the result is undefined and neither .c nor .d is read.
?? for defaultsOptional chaining returns undefined for missing data. Pair it with the nullish coalescing operator ?? to supply a default.
Use ?? and not ||, so that valid values such as 0 or an empty string are not replaced.
Optional chaining cannot be used on the left side of an assignment.
It also has these limits.
| Case | Behaviour |
|---|---|
delete obj?.prop | Allowed; returns true when obj is nullish |
new a?.b() | SyntaxError |
a?.b where a is 0 or "" | Not short-circuited; only null and undefined are |
undeclared?.x | ReferenceError; the variable must exist |
Do not put ?. everywhere. If a value is required by your program, a missing value is a bug, and a silent undefined hides it. Use optional chaining for data that is genuinely optional, and validate required data at the boundary.
Because the result is undefined, a failure can look the same as a real undefined value. If the difference matters, check explicitly with in or Object.hasOwn().
It returns undefined, not null, and it stops evaluating the rest of the chain.
Yes. fn?.() calls fn only if it is not null or undefined, and obj.method?.() does the same for methods.
No. It only guards against null and undefined in a path. Other errors, such as calling a non-function, are still thrown.
Not for current browsers and Node.js 14 and later. A build step is needed only if you must support very old environments.
Optional chaining has three forms: obj?.prop, obj?.[expr] and fn?.(). It returns undefined and skips the rest of the chain when the left side is null or undefined. Combine it with ?? for defaults, avoid it on assignment targets, and keep it for data that is truly optional.