Reading a property of undefined throws: user.address.city crashes when there is no address.
Optional chaining?. checks the value on its left. If it is null or undefined, the whole expression returns undefined instead of throwing.
It works for properties (user?.name), brackets (list?.[0]) and calls (user.greet?.()).
Combine it with a fallback: user.address?.city ?? "Unknown".
Optional chaining short-circuits: in a?.b.c.d, if a is nullish nothing to the right is evaluated.
Only put ?. where a missing value is expected and valid. Scattering it everywhere hides real bugs, such as a misspelled property that should always exist.
?. cannot be used on the left of an assignment: user?.name = "x" is a SyntaxError.
It treats only null and undefined as missing: ""?.length is 0, and 0?.toFixed(1) works.
05Definitions
Optional chaining
The ?. operator, which returns undefined instead of throwing when the left side is null or undefined.
Nullish
Either null or undefined.
Short-circuit
Stopping evaluation early once the result is known.
Profile data arrives from several services and optional sections are often missing. Optional chaining reads those sections safely, while required fields are read normally so their absence is still noticed.
6. An order must always have an id. Which read is better?
order?.id?.toString()
order.id.toString()
order?.["id"]
String(order?.id ?? "")
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Stop the crash: Prints both lines
First tag or none: Prints both; Empty list; Null tags
Shipping label: Prints both labels; No order
ex-043-01: output matches the documented expected output
ex-043-02: output matches the documented expected output
ex-043-03: output matches the documented expected output
ex-043-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-09-modern-javascript-syntax/lesson-043-optional-chaining/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
?. returns undefined instead of throwing when the left side is nullish.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationoptional chaining
Context: I am learning optional chaining in JavaScript.
Task: Explain optional chaining with a customer address example, including short-circuiting.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionoptional chaining drill
Context: I have practised optional chaining.
Task: Give me six snippets with ?. in different positions and ask which throw and which print undefined.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingoptional chaining bug
Context: I added ?. but my code still throws TypeError: Cannot read properties of undefined.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationoptional chaining practice
Context: I finished the optional chaining exercises.
Task: Create three exercises that read partial API responses safely.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewoptional chaining review
Context: Here is my code that uses optional chaining.
Task: Review my code for places where ?. hides bugs and places where it is genuinely needed.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.