Skip to main content
The && operator performs a logical conjunction on two boolean operands. It evaluates to true if and only if both the left-hand side (LHS) and the right-hand side (RHS) expressions evaluate to true.

Syntax

expression1 && expression2

Truth Table

Expression AExpression BResult (A && B)
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Short-Circuit Evaluation

The && operator utilizes short-circuit evaluation (lazy evaluation). The runtime evaluates the LHS operand first:
  1. If the LHS evaluates to false, the entire expression immediately resolves to false. The RHS is not evaluated.
  2. If the LHS evaluates to true, the runtime proceeds to evaluate the RHS. The result of the entire expression is then determined by the value of the RHS.
This behavior prevents side effects or computations defined in the RHS from executing if the LHS condition is not met.
bool checkLHS() {
  print('LHS checked');
  return false;
}

bool checkRHS() {
  print('RHS checked');
  return true;
}

void main() {
  // Only "LHS checked" is printed.
  // checkRHS() is never executed because checkLHS() returns false.
  bool result = checkLHS() && checkRHS(); 
}

Type Safety

Dart is a strongly typed language that does not support implicit “truthy” or “falsy” coercion for operator definitions. The && operator is explicitly defined only for the bool type. Attempting to use the operator on non-boolean types results in a compile-time error.
int x = 1;
int y = 2;

// Compile-time error: The operator '&&' isn't defined for the type 'int'.
// if (x && y) { ... } 

// Correct usage: Explicit boolean comparison
if (x > 0 && y > 0) { 
  // ...
}

Nullability

Both operands must resolve to a non-nullable bool. Because Dart enables sound null safety, a nullable boolean (bool?) cannot be used directly with the && operator without explicit unwrapping or null checking.
bool? isReady;

// Compile-time error: The argument type 'bool?' can't be assigned to the parameter type 'bool'.
// var result = true && isReady; 

// Correct usage: Handle null explicitly (e.g., default to false)
var result = true && (isReady ?? false);

Precedence

The && operator has lower precedence than equality operators (==, !=) but higher precedence than the logical OR operator (||).
// Evaluated as: (A && B) || C
bool result = A && B || C;
Master Dart with Deep Grasping Methodology!Learn More