| operator in Dart serves as the bitwise inclusive OR operator for integers, a non-short-circuiting logical OR operator for booleans, and an overloadable operator for custom classes.
Integer Bitwise OR
When applied toint operands, the | operator evaluates the binary representations of both operands position by position. If at least one of the corresponding bits is 1, the resulting bit is 1. If both bits are 0, the resulting bit is 0. The operation returns an int.
Truth Table:
0 | 0yields00 | 1yields11 | 0yields11 | 1yields1
- Dart Native (AOT/JIT): Bitwise operations are performed on 64-bit two’s complement integers.
- Dart Web (compiled to JavaScript): Bitwise operations are truncated to 32-bit integers to align with JavaScript’s underlying bitwise semantics.
Boolean Non-Short-Circuiting OR
When applied tobool operands, the | operator performs a logical OR operation returning a bool. Unlike the standard logical OR operator (||), the | operator does not short-circuit. It strictly evaluates both the left and right expressions regardless of whether the left expression evaluates to true.
Operator Overloading
Dart supports operator overloading, allowing user-defined classes to implement the| operator. The parameter type and the return type are entirely dictated by the class definition.
Compound Assignment
The| operator can be combined with the assignment operator (=) to mutate a variable in place using the |= syntax. The variable being assigned to must be mutable (not final or const).
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





