Skip to main content
The logical OR operator (||) performs a disjunction on two boolean expressions. It evaluates to true if at least one of the operands is true; otherwise, it evaluates to false.

Syntax

expression1 || expression2

Truth Table

Operand AOperand BResult
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Short-Circuit Evaluation

The || operator evaluates operands from left to right and employs short-circuit logic.
  1. Left Operand Evaluation: The left operand is evaluated first.
  2. Short-Circuiting: If the left operand resolves to true, the entire expression immediately resolves to true. The right operand is not evaluated, and any side effects (such as function calls or variable assignments) contained within the right operand are skipped.
  3. Right Operand Evaluation: If the left operand resolves to false, the right operand is evaluated, and its result determines the final value of the expression.
bool returnTrue() {
  print("Left operand evaluated");
  return true;
}

bool returnFalse() {
  print("Right operand evaluated");
  return false;
}

void main() {
  // The right operand is skipped because the left is true.
  // Output: "Left operand evaluated"
  bool result = returnTrue() || returnFalse();
}

Type Constraints

Dart is a strongly typed language. Both operands of the || operator must be of type bool. Unlike languages with “truthy” or “falsy” coercion (e.g., JavaScript), Dart throws a compile-time error if non-boolean types (such as int or String) are used as operands.
// Valid
bool isValid = (5 > 3) || (2 == 2);

// Invalid (Compile-time error)
// bool isInvalid = "string" || 1;

Operator Precedence

The || operator has lower precedence than the logical AND operator (&&) and equality operators (==, !=). In complex expressions without parentheses, && operations are evaluated before || operations.
// Equivalent to: true || (false && false)
// Result: true
bool result = true || false && false;
Master Dart with Deep Grasping Methodology!Learn More