Syntax
const city = user?.address?.city ?? "Unknown";Examples
Safely reading nested data
A small, runnable example of this syntax.
const user = { name: "Ada" };
const city = user.address?.city ?? "Unknown";
console.log(city); // UnknownBest practices
- Use ?? when 0, false, or an empty string are valid values; use || only when all falsey values should trigger the fallback.
- Keep examples small while learning, then combine the idea with a real project.
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification