Optional Chaining with the ?. Operator
Safely access deeply nested properties.
Optional Chaining with the ?. Operator is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Optional Chaining with the ?. Operator” lesson free?
Yes — the full text of “Optional Chaining with the ?. Operator” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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