Search⌘ K
AI Features

Operator Precedence and Associativity

Explore how Dart's operator precedence controls the order of evaluation in expressions and how associativity resolves evaluation order when operators share precedence. Learn to predict complex expression results accurately by understanding these essential concepts, which are vital for writing clean and reliable Dart code.

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

-, !, ~, ++, --, await

Multiplicative

*, /, ~/, %

Additive

+, -

Shift

<<, >>, >>>

Bitwise AND

&

Bitwise XOR

^

Bitwise OR

`

Relational and type test

<, >, <=, >=, as, is, is!

Equality

==, !=

Logical AND

&&

Logical OR

||

If-null

??

Conditional

? :

Cascade

.., ?..

Assignment

=, *=, /=, +=, -=, &=, ^=, etc.

We will ...