Optional Chaining with the ?. Operator
Safely access deeply nested properties.
Optional Chaining with the ?. Operator is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
The Problem
Reading a property on something that is undefined or null throws a TypeError. Optional chaining lets you read nested values safely.
const user = {};
console.log(user.profile && user.profile.name);The ?. Operator
The optional chaining operator ?. stops and returns undefined if the value to its left is null or undefined, instead of throwing.
const user = {};
console.log(user.profile?.name); // undefined, no errorReading Deep Properties
You can chain several ?. steps to drill into deep structures without manual guards at each level.
const data = { user: { address: { city: "Paris" } } };
console.log(data.user?.address?.city); // "Paris"Short-circuiting
If any link in the chain is nullish, the whole expression short-circuits to undefined and the rest is not evaluated.
const data = { user: null };
console.log(data.user?.address?.city); // undefinedOnly Nullish Triggers It
?. only short-circuits on null or undefined. Other falsy values like 0 or "" are real values and are accessed normally.
const obj = { count: 0 };
console.log(obj.count?.toString()); // "0"Comparison to &&
Before ?., people chained && guards. Optional chaining is shorter and clearer for the same intent.
const u = { profile: { name: "Ada" } };
const a = u && u.profile && u.profile.name;
const b = u?.profile?.name;
console.log(a, b);Chaining After a Call
You can continue chaining after a method call, accessing properties on the returned value safely.
const config = { get: () => ({ port: 8080 }) };
console.log(config.get()?.port); // 8080Mid-chain Short-circuit
If a middle link is nullish, evaluation stops immediately, even if later links would otherwise call functions.
const obj = { a: null };
console.log(obj.a?.b.c.d); // undefined, never touches b/c/dNot for Assignment
Optional chaining is for reading. You cannot assign to an optional chain; it is a syntax error on the left side of =.
const o = { x: 1 };
// o?.x = 5; // SyntaxError
o.x = 5;
console.log(o.x);Combining With Arrays
It works with bracket access too, which is covered more later. The same nullish-safety applies.
const state = { items: ["a", "b"] };
console.log(state.items?.[0]); // "a"Avoiding Overuse
Use ?. only where a value can legitimately be missing. Overusing it can hide bugs by silencing errors you actually want to see.
const order = { total: 42 };
console.log(order.total?.toFixed(2)); // "42.00"Quick Check
Predict the result.
Recap
The ?. operator safely reads properties: if the left side is null or undefined it returns undefined instead of throwing, short-circuiting the rest of the chain. Only nullish values trigger it (not 0 or ""), and it cannot be used as an assignment target.
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Optional Chaining with the ?. Operator” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Optional Chaining with the ?. Operator”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
What will I learn in “Optional Chaining with the ?. Operator”?
Safely access deeply nested properties. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Optional Chaining with the ?. Operator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Optional Chaining with the ?. Operator
- Nullish Coalescing with ??
- Combining ?. and ??
- Optional Calls and Index Access