lecture 13 of 150 completed

Optional chaining and nullish coalescing

?. reads a value that may be missing. || replaces a real 0 or false with the default, and ?? does not.

What you'll learn

  • Why || turns a retry limit of 0 into the default
  • What ?? does differently
  • When ?. helps, and when it hides a failure

A test helper retries a flaky step up to 3 times by default. One test must not retry at all, so it sets the limit to 0. The helper retries 3 times anyway.

const options = { retries: 0 };

const retries = options.retries || 3;
console.log(retries);
3

|| means "or". a || b gives a when a counts as true, and b when it does not. People use it to supply a default.

The trouble is the phrase "counts as true". From the equality lecture, 0, "" and false all count as false. So || throws away a real 0 and uses the default.

The setting was valid. The helper ignored it, and no error appeared.

?? replaces only what is missing

?? is the nullish coalescing operator. a ?? b gives b only when a is null or undefined. Every other value is kept, including 0, "" and false.

const options = { retries: 0, headless: false };

console.log(options.retries ?? 3);
console.log(options.headless ?? true);
console.log(options.timeout ?? 5000);
0
false
5000

The first two settings exist, so they are kept. timeout is not set, so it reads as undefined and the default is used.

Use ?? for defaults. Use || only when you really want to replace every falsy value.

?. reads through a value that may be missing

In the last lecture, reading a property of undefined stopped the code. Optional chaining is the ?. operator. It checks the value on its left first. If that value is null or undefined, it stops there and gives undefined.

const loan = { book: { title: "Dune" } };
const empty = {};

console.log(loan.book?.title);
console.log(empty.book?.title);
console.log(empty.book?.shelf?.code ?? "no shelf");
Dune
undefined
no shelf
  1. loan.book exists, so ?.title reads the title as a normal dot would.
  2. empty.book is undefined, so the whole expression is undefined. There is no error.
  3. You can chain several, and finish with ?? to supply a default.

This is the right tool for data where a missing part is normal. A reader may have no address. A booking may have no room yet.

Do not use ?. to quiet a check

?. turns an error into undefined. In a check, that can turn a failure into a pass.

const actual = undefined;
const expected = undefined;

console.log(actual?.status === expected?.status);
true

Both sides failed to load. Both read as undefined, and undefined === undefined is true. With a plain dot, this line would have stopped with a TypeError, and the test would have failed as it should.

A reviewer who sees ?. inside an assertion will ask one thing: is this value allowed to be missing? If it is not, use a plain dot and let the error show.

Remember this

|| replaces 0, "" and false. ?? replaces only null and undefined, so use it for defaults. ?. is for values that may be missing on purpose, and it does not belong in a check that should fail.

Reference: Nullish coalescing operator on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.