Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Optional Chaining

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 119: Optional Chaining (?.)

Implement `safeCity(user)` returning `user.address.city` via optional chaining, defaulting to `N/A` when missing.

Sample tests

Input: safeCity({"address":{"city":"NYC"}})
Output: "NYC"
Input: safeCity({})
Output: "N/A"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Accessing nested properties without ?. throws on a missing parent.
Visualize this concept: Optional Chaining and Nullish Coalescing →
Approach & explanation (try first)

Optional chaining short-circuits to undefined on a missing link, then ?? supplies the default.

Loading...
⌘/Ctrl + Enter

Run your code to see results.