ELI5: Explain Like I'm 5

Operator associativity

Okay kiddo, do you remember when we learned about adding, subtracting, multiplying, and dividing numbers? We learned that we need to do them in a certain order to get the right answer, right? That's called "operator precedence." But sometimes there are more than one of the same kind of operator in an equation, and we need to know which one to do first. That's where "operator associativity" comes in.

Operator associativity tells us which way to group the operators - whether we do the ones on the left first or the ones on the right first. For example, if we have an equation like 2 + 3 + 4, we can either add 2 and 3 first, or add 3 and 4 first. The answer will be the same, but we need to have a rule for deciding which way to do it.

Most operators have what's called "left associativity," which means that we do the ones on the left first. So in our example, we would add 2 and 3 first, getting 5, and then add 4 to that, getting the final answer of 9.

But some operators, like the exponentiation operator (which looks like this: ^), have "right associativity," which means we do the ones on the right first. So if we have an equation like 2^3^2, we would start by doing 3^2 first, getting 9, and then do 2^9, which is a really big number (512!).

So the next time you see a bunch of operators in an equation, remember that operator associativity tells us which way to group them so we can get the right answer!
Related topics others have asked about: