nullish coalescing
The double question mark operator (??), which gives a fallback value only when the left side is null or undefined.
In more detail
Unlike ||, it keeps other falsy values such as 0 and the empty string, so retries ?? 3 keeps a real 0. Nullish means null or undefined.
Example
const ip = entry.metadata["client_ip"] ?? "unknown";Related terms
- optional chaining — The operator written (?.) that reads a property or calls a method only if the value before it is not null or undefined.
- falsy — A value that counts as false in a condition: false, 0, -0, 0n, the empty string, null, undefined and NaN.
- null — A special value meaning no value or no object, set on purpose to show that something is empty or absent.
- undefined — The JavaScript value meaning not set: a variable with no value yet, a missing property or a function that returned nothing.
Taught in
- The Compiler Pipeline: tsc, AST And Code Emission — Production TypeScript
- Typeof Guards, Truthiness And Equality Narrowing — Production TypeScript
- Union Utilities: Exclude, Extract, NonNullable And Awaited — Production TypeScript