Optional chaining and nullish coalescing are modern JavaScript features introduced to simplify working with missing or undefined data.
These features help developers write safer and cleaner code, especially when working with:
- API responses
- User-generated data
- Nested objects
- Optional configuration values
- Dynamic application state
Optional chaining prevents runtime errors when accessing deeply nested properties, while nullish coalescing provides safer default values.
The Problem with Nested Properties
In JavaScript, accessing properties on undefined or null values causes runtime errors.
For example:
const user = {};
console.log(user.profile.name);
This code throws an error because
profile
does not exist.
Before optional chaining, developers often wrote multiple checks manually.
What is Optional Chaining?
Optional chaining uses the
?.
operator to safely access nested properties.
If a property does not exist,
JavaScript returns
undefined
instead of throwing an error.
const user = {};
console.log(user.profile?.name);
Since
profile
does not exist,
the result becomes
undefined
instead of crashing the application.
Accessing Deeply Nested Data
Optional chaining becomes especially useful when working with deeply nested objects.
const user = {
profile: {
address: {
city: "Hyderabad"
}
}
};
console.log(user.profile?.address?.city);
Each level is checked safely before accessing the next property.
Optional Chaining with Arrays
Optional chaining also works with arrays.
const users = [];
console.log(users[0]?.name);
If the array item does not exist,
JavaScript safely returns
undefined.
Optional Chaining with Functions
Optional chaining can safely call functions that may not exist.
const user = {
greet: () => {
console.log("Hello");
}
};
user.greet?.();
If
greet
does not exist,
JavaScript avoids throwing an error.
What is Nullish Coalescing?
Nullish coalescing uses the
??
operator to provide default values.
It only falls back when the value is:
nullundefined
This behavior is different from the logical OR operator
||.
Basic Nullish Coalescing Example
const username = null;
const displayName = username ?? "Guest";
console.log(displayName);
Since
username
is null,
the default value
"Guest"
is used.
Difference Between || and ??
Many beginners confuse
||
and
??.
The logical OR operator treats many values as falsy, including:
0""false
Nullish coalescing only checks for:
nullundefined
const count = 0;
console.log(count || 10);
console.log(count ?? 10);
The first example returns
10,
while the second correctly returns
0.
Combining Optional Chaining and Nullish Coalescing
These features are often used together when working with API responses.
const user = {};
const city =
user.profile?.address?.city ?? "Unknown City";
console.log(city);
This approach prevents runtime errors and provides safe fallback values.
Real-World Example
Modern applications frequently receive incomplete API data. Optional chaining and nullish coalescing help handle missing information safely.
const apiResponse = {
user: {
name: "Sarah"
}
};
const email =
apiResponse.user?.email ?? "No email available";
console.log(email);
Common Beginner Mistakes
- Confusing
||with?? - Using optional chaining unnecessarily everywhere
- Assuming optional chaining fixes all runtime errors
- Forgetting that optional chaining returns undefined
Best Practices
- Use optional chaining for uncertain or external data
- Use nullish coalescing for safe fallback values
- Avoid excessive chaining when data structure is guaranteed
- Keep fallback values meaningful and user-friendly
Summary
Optional chaining and nullish coalescing simplify working with missing or undefined data in JavaScript. They help developers write safer, cleaner, and more maintainable applications.
These features are now widely used in modern frontend and backend development, especially when handling API responses and dynamic application state.