The logical OR (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
||) operator evaluates operands from left to right and returns the first truthy operand it encounters. If all operands evaluate to falsy values, it returns the value of the last operand. It utilizes short-circuit evaluation, meaning the runtime bypasses the evaluation of subsequent operands immediately after a truthy value is resolved.
Evaluation Mechanics
The operator relies on JavaScript’s underlying boolean coercion rules. The following values are strictly evaluated as falsy:false, 0, -0, 0n, "" (empty string), null, undefined, and NaN. All other values are evaluated as truthy.
- If
expr1can be coerced totrue(truthy), the expression returnsexpr1.expr2is never evaluated. - If
expr1can be coerced tofalse(falsy), the expression evaluates and returnsexpr2.
TypeScript Type Resolution
At compile time, TypeScript infers the return type of a|| expression as a union of the types of its operands.
Control Flow Analysis and Type Narrowing
TypeScript’s compiler uses the|| operator to perform control flow analysis and type narrowing. When the || operator is evaluated, TypeScript automatically removes types known to be falsy (such as null or undefined) from the resulting type union, provided the right-hand operand does not also contain those types.
"" or 0), TypeScript will also exclude those specific literal types from the left side of the resulting union.
Operator Chaining
Multiple|| operators can be chained sequentially. The short-circuit evaluation remains strictly left-to-right, and the TypeScript compiler expands the union type to include all potential return values across the chain.
Master TypeScript with Deep Grasping Methodology!Learn More





