← Practice
Modern JavaScript·easy 10 min 50 XP

Optional Chaining

Write getCity(user) that safely returns user.address.city, or undefined if address is missing, using optional chaining.

Examples

  • getCity({ address: { city: "Delhi" } }) → "Delhi"
  • getCity({}) → undefined

Constraints

  • Keep the required function name so the tests can find it
  • Return a value instead of only logging it
  • Do not mutate inputs unless the challenge asks you to
Optional Chaining · editor

Code runs in an isolated Web Worker with a 3-second timeout — no DOM access, and it can never hang the page.