Zest.

JavaScript: Optional chaining and nullish coalescing

A short, tested JavaScript example for optional chaining and nullish coalescing. Copy it and adjust the names and paths for your project.

JavaScript
const city = user?.address?.city ?? "Unknown";
const first = list?.[0];
const result = obj.method?.();

// ?? only replaces null/undefined, || replaces any falsy value
0 ?? 5;  // 0
0 || 5;  // 5

Find it in Zest

Search for “javascript optional chaining and nullish coalescing” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More JavaScript examples

All JavaScript examples