javascript
26 lines · 5 steps
Optional chaining and nullish coalescing in JS
How ?. and ?? let you safely reach into nested objects and supply fallbacks without falsy bugs.
Explained by
highlit
1function getCityName(user) {
2 return user?.address?.city ?? "Unknown city";
3}
4
5function getFirstHobby(user) {
6 return user?.profile?.hobbies?.[0] ?? "No hobbies listed";
7}
8
9function invokeFormatter(user) {
10 return user?.settings?.formatName?.() ?? "default formatter";
11}
12
13function resolveConfig(options) {
14 const timeout = options?.timeout ?? 3000;
15 const retries = options?.retries ?? 0;
16 const headers = options?.headers ?? {};
17 return { timeout, retries, headers };
18}
19
20function deepValue(obj, fallback) {
21 // ?. short-circuits to undefined the moment a link is null/undefined,
22 // ?? only falls back on null or undefined (not 0, '', or false).
23 const count = obj?.stats?.count ?? fallback;
24 const isActive = obj?.flags?.active ?? false;
25 return { count, isActive };
26}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Optional chaining short-circuits to undefined the instant any link in the path is null or undefined, avoiding 'cannot read property of undefined' errors.
- 2Nullish coalescing only falls back on null or undefined, so legitimate values like 0, '', and false survive untouched.
- 3Combining ?. and ?? gives concise, safe access to deep structures with explicit defaults at the call site.
Related explainers
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
javascript
import { useState, useEffect, useCallback, useRef } from "react"; export function usePersistentForm(storageKey, initialValues) { const [values, setValues] = useState(() => {
A React hook that persists form state to localStorage
custom-hooks
localstorage
debounce
Intermediate
9 steps
javascript
function groupBy(items, keySelector) { const resolveKey = typeof keySelector === 'function' ? keySelector : (item) => item[keySelector];
Building a flexible groupBy in JavaScript
higher-order-functions
reduce
data-transformation
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/optional-chaining-and-nullish-coalescing-in-js-explained-javascript-d612/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.