Optional Chaining & Nullish Coalescing in JavaScript

From the JavaScript cheat sheet · Modern JavaScript (ES6+) · verified Jul 2026

Optional Chaining & Nullish Coalescing

Safe property access and default values

javascript
// Optional chaining
const city = user?.address?.city;
const result = obj.method?.();
const item = arr?.[index];

// Nullish coalescing
const value = input ?? 'default';
const port = process.env.PORT ?? 3000;
💡 ?. stops evaluation and returns undefined if null/undefined
⚡ ?? only replaces null/undefined, not other falsy values
📌 Use ?? for numeric values that could be 0
🔥 Combine ?. and ?? for safe access with defaults

Continue with JavaScript

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full JavaScript cheat sheet