Loading video player…

Choosing Between || and ??

Choose the operator from the field’s meaning. For Millbrook’s phone field, an empty string means no number, so || tries the next source. For a deposit, zero is a real waiver, so ?? keeps it and defaults only for null or undefined.

The final phone and deposit lines use the current booking r and its saved regular-hirer setting regular, read from REGULARS.get(r.name). With DEFAULT_DEPOSIT set to 50, the choir gets its saved phone and a zero deposit.

Language: JavaScript
const phone = (
  r.phone
  || regular?.phone
  || "no phone given"
);
const deposit = regular?.deposit ?? DEFAULT_DEPOSIT;

?? preserves 0, "", false, and NaN; || uses the fallback for those values. Both use the fallback for null and undefined. Neither operator validates input. Python’s dictionary default covers a missing key; ?? also covers an explicit null.

Keep one operator throughout a chain, or group the part that goes first with parentheses, as in (r.phone || regular?.phone) ?? "no phone given".

Next up: Fill In the Setup Sheet. Prepare the caretaker’s equipment and entrance for each booking so every setup line matches the Python script.

00:00 You’ll choose the right default for Millbrook’s phone numbers. Nadia needs a number to call before confirming each booking. The choir left its phone box blank, but its saved settings contain a number.

00:13 In Python, the existing chain checks the request, then the regular’s number, then a message.

00:25 An empty string is false, so Python prints 555-0142 here. In JavaScript, the form still supplies an empty string. The task is to preserve that contact policy.

00:40 You may already use nullish coalescing for a missing value: it takes a fallback for null or undefined. In the JavaScript port, optional access reads the saved phone if a regular setting exists.

00:54 This candidate uses the nullish default at both links, trying the same three sources as Python.

01:05 What will the choir’s phone line print? Your turn: pause the video and predict the phone line.

01:17 It prints the phone label, then nothing after it.

01:22 The request supplied an empty string. That is a value in its own right, neither null nor undefined. The first nullish operator keeps it, so this chain never needs the saved number or the final message.

01:37 Python’s or continues past this empty string because it is false. The difference comes from what each operator counts as missing, rather than the order of the sources.

01:50 For this phone field, blank means no number was supplied. JavaScript’s || matches that policy: it uses a fallback when the current value is falsy.

02:02 Replace both nullish operators with ||, keeping the sources in the same order. The empty request phone now gives way to the saved number. The choir’s phone line prints 555-0142 again.

02:19 The same operator at each link describes one consistent policy for this field. For Maya Okafor, the request phone is also blank, and there is no regular setting.

02:32 Optional access gives undefined for that missing saved phone.

02:37 The same || chain reaches the last source and prints no phone given. A supplied phone is different. Tom, Tuesday Yoga, and Ana each have a number on the request, so that first value stays selected.

02:53 The policy is the same across all five bookings. Now keep the choir’s waived deposit. Zero is a real amount here, so the nullish default is the right policy.

03:06 In Python, the dictionary default supplies fifty when the deposit key is absent.

03:19 Here is the JavaScript expression and its familiar display call.

03:33 It keeps the choir’s zero and prints a deposit of zero dollars. Watch out: || would replace that zero with fifty. Nullish coalescing also covers an explicit null, while Python’s dictionary default covers only a missing key.

03:53 Here is the same choice as a table, with the string "d" as the fallback. For zero, || gives "d"; the nullish default keeps zero.

04:05 An empty string and false follow that same split: || gives "d", while the nullish default keeps the original value.

04:14 For null and undefined, both operators give "d". For NaN (not a number), || gives "d", while the nullish default keeps that value.

04:26 These policies select values; neither one validates a phone or an amount. Ask what counts as missing for each field. For the phone, a blank string means no number, so use ||.

04:40 For the deposit, zero is real, so use the nullish default. Watch out: mixing these operators needs parentheses around the part that goes first, as the grouped phone expression shows, or keep one operator throughout the chain.

04:58 Next up: Fill In the Setup Sheet. You’ll prepare the caretaker’s equipment and entrance for each booking, so every setup line matches the Python script.

Become a Member to join the conversation.