Skip to main content
The || (logical OR) operator is a binary boolean operator that evaluates two expressions and returns true if at least one of the operands evaluates to true. It returns false strictly when both operands evaluate to false.
expression1 || expression2

Technical Characteristics

  • Strict Type Requirement: Both operands must evaluate to the bool type. Unlike languages with “truthy” or “falsy” concepts (e.g., JavaScript), Dart enforces strict type checking and does not perform implicit type coercion on non-boolean values.
  • Short-Circuit Evaluation: Dart evaluates the || operator using short-circuit logic from left to right. If the left operand evaluates to true, the operator immediately yields true and the right operand is never evaluated.
  • Associativity: Left-to-right.
  • Precedence: The || operator has lower precedence than the logical AND (&&) operator and relational operators (such as ==, !=, <), but higher precedence than assignment operators (=).

Evaluation Mechanics

The following demonstrates the standard truth table outcomes:
bool a = true || true;   // true
bool b = true || false;  // true
bool c = false || true;  // true
bool d = false || false; // false

Short-Circuit Behavior

Because of short-circuit evaluation, the right-hand expression is entirely bypassed if the left-hand expression satisfies the true condition. This mechanism prevents the execution of functions, state mutations, or potential runtime errors present in the second operand.
bool executeSideEffect() {
  print('Right operand evaluated');
  return true;
}

void main() {
  // The left operand is true, so short-circuiting occurs.
  // executeSideEffect() is never called, and nothing is printed.
  bool result1 = true || executeSideEffect(); 

  // The left operand is false, so the right operand MUST be evaluated.
  // "Right operand evaluated" is printed to the console.
  bool result2 = false || executeSideEffect(); 
}

Strict Boolean Enforcement

Attempting to use non-boolean types with the || operator results in a compile-time error. Expressions must be explicitly resolved to bool.
// INVALID: Compile-time error
// Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
bool invalid = 1 || 0; 
bool invalidString = "text" || "";

// VALID: Explicit boolean expressions are required
bool valid = (1 > 0) || ("text".isNotEmpty); 
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More