optional chaining

The operator written (?.) that reads a property or calls a method only if the value before it is not null or undefined.

In more detail

req.user?.role gives undefined instead of crashing when user is missing. It is JavaScript syntax from ES2020 that TypeScript understands and narrows correctly.

Example

const role = req.user?.role ?? "anonymous";

Related terms

Taught in

References