liveBook · Manning

Logical OR

As explained in Pro Angular 16

The logical OR operator is a JavaScript operator that returns its left operand when truthy and otherwise returns its right operand.

The logical OR operator (||) returns its left operand when that value is truthy; otherwise, it returns the right operand. This makes it useful for fallback values, especially when a variable may be undefined. section 3.4.7

Fallback values

With val1 undefined, val1 || "fallback value" returns "fallback value". With val2 set to "London", val2 || "fallback value" returns "London". However, val3 || 100 returns 100 when val3 is 0, because 0 is falsy. section 3.4.7

Table 3.3. Useful JavaScript operators
Operator
Description
++, --
Pre- or post-increment and decrement
+, -, *, /, %
Addition, subtraction, multiplication, division, remainder
<, <=, >, >=
Less than, less than or equal to, more than, more than or equal to
==, !=
Equality and inequality tests
===, !==
Identity and nonidentity tests
&&, ||
Logical AND and OR
||, ??
Null and null-ish coalescing operators
?
Optional chaining operator
=
Assignment
+
String concatenation
?:
Three-operand conditional statement

Compare logical OR with logical AND, nullish coalescing, optional chaining, conditional, and assignment operators. section 3.4.7

Use || cautiously when valid values may be falsy. The nullish-coalescing operator (??) preserves 0 while still providing a fallback for null or undefined. section 3.4.7

How Pro Angular 16 covers it

How deep it goes
Explains operand selection and demonstrates the falsy-value limitation, but does not descend into operator implementation internals.
Examples
Uses undefined strings, the value "London", and zero with fallback expressions producing console output.
What it assumes
Assumes basic JavaScript or TypeScript variables and introduces truthy, falsy, undefined, and nullish fallback behavior.
What you can do after
Apply logical OR to select fallback values and recognize when falsy data makes nullish coalescing more appropriate.
How it connects
Connects logical OR with truthy and falsy values, nullish coalescing, logical AND, optional chaining, and conditional operators.

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 Pro Angular 16. The section links point to the parts of the book it draws on.