liveBook · Manning

Logical OR

As explained in Essential TypeScript 5, Third Edition

The logical OR operator evaluates alternatives using JavaScript truthiness and produces a fallback when the left operand is falsy.

The logical OR operator (||) evaluates the left operand using JavaScript truthiness. It returns that value when truthy; otherwise, it returns the right-hand fallback. section 3.3.2

Supplying a default city

An unassigned variable has the value undefined, so the fallback is selected: let firstCity; let secondCity = firstCity || "London";. The result is London. section 3.3.2

Use || carefully when zero, an empty string, NaN, or false are meaningful values. Each is falsy, so the fallback replaces it. A zero tax rate therefore becomes 10% in the example. section 3.3.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines fallback behavior and falsy edge cases, then contrasts nullish coalescing without descending into operator internals.
Examples
Uses an undefined city and a zero tax rate to demonstrate successful and surprising fallback behavior.
What it assumes
Assumes JavaScript variables and values while explaining undefined, null, boolean coercion, and the complete set of relevant falsy values.
What you can do after
Apply it via JavaScript expressions to provide defaults while recognizing when falsy values require nullish coalescing instead.
How it connects
Connects type coercion with nullish coalescing and optional chaining for safer property and fallback checks.

Also explained in

Compare how 6 books explain logical OR →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.