Assignment Has Lowest Precedence
What you'll learn: Why assignment operators always evaluate last, allowing you to chain assignments and build complex expressions safely.
The Core Idea
In JavaScript's order of operations, assignment operators (like =, +=, -=) have the lowest precedence of almost all operators. This means everything else in an expression gets calculated first, and then the final result gets assigned to the variable.
Think of assignment like the final step of wrapping a gift: you gather all the materials, fold the paper, add the ribbon, and only then do you hand the finished package to someone. Assignment is that last "handoff."
Why This Matters
Because assignment happens last, you can write expressions like this:
let x = 5 + 3 * 2;
Here's what happens in order:
3 * 2evaluates to6(multiplication has higher precedence)5 + 6evaluates to11(addition happens next)11is assigned tox(assignment happens last)
Right-to-Left Assignment Chains
This low precedence also enables chaining assignments:
let x, y, z;
x = y = z = 0;
Because assignment evaluates right-to-left and returns the assigned value, this works like:
z = 0(z becomes 0, expression returns 0)y = 0(y becomes 0, expression returns 0)x = 0(x becomes 0)
All three variables end up with the value 0.
Key Takeaway: Assignment operators have the lowest precedence, so they always happen after calculations are complete. This lets you write clean expressions and chain multiple assignments together.