Operator Precedence and Associativity
Explore how Dart evaluates complex expressions by applying operator precedence and associativity rules. Understand how to control operation order using parentheses and how Dart resolves operators with equal precedence. This lesson helps you write clearer, more predictable Dart code.
We'll cover the following...
Operator precedence determines the order in which the Dart engine evaluates different parts of an expression. For instance, the mathematical expression 1 + 1 * 5 yields 6 rather than 10. This happens because the multiplication operator (*) has higher precedence than the addition operator (+).
If we want the addition to happen first, we can use parentheses to group the operation as (1 + 1) * 5. Parentheses have the highest precedence and force the enclosed expression to evaluate first.
Precedence table
Below is the precedence hierarchy for operators in Dart. Operators at the top have the highest precedence, meaning they evaluate first. Each operator has a higher precedence than the operators in the rows that follow it.
Description | Operator |
Unary postfix |
|
Unary prefix |
|
Multiplicative |
|
Additive |
|
Shift |
|
Bitwise AND |
|
Bitwise XOR |
|
Bitwise OR | ` |
Relational and type test |
|
Equality |
|
Logical AND |
|
Logical OR |
|
If-null |
|
Conditional |
|
Cascade |
|
Assignment |
|
We will discuss some of the ...