In math, the = sign represents the equality comparison operator, an assertion that "the thing on the left has the same value as the thing on the right."
This operator is commutative: you can exchange the left & right sides without changing anything (the statement that the left & right sides have the same value remains as true or as false as it was before. Reading them in a different order doesn't change its truth value)
In programming languages like Lua, the equality comparison operator is written
==, with two equals signs.
In these languages, when you write just one
=, you're not doing an equality comparison at all. This is a completely different operation: an assignment operator.
The assignment operator
= says "take the value of the thing on the right, and assign it to the variable on the left"
It's the difference between "check whether these things are equal" (
==) versus "make these two things equal" (
But of course there are two ways we could make the things equal by changing the value of one side: the left or the right. So, we say that the assignment operator always changes the left side. That means that assignment is not commutative. Observe:
local a = 1
local b = 2
if aFirstThenBSecond then
a = b
b = a
If we take the first branch, we're saying "take the value of
b and store it in variable
a," so both variables hold the value 2 when this code finishes executing.
If we take the second branch, we're saying "take the value of
a and store it in variable
b," so both variables hold the value 1 when this code finishes executing.
In both cases,
b are equal to one another at the end, so the meaning of
= as "make these things equal" holds both ways.
We get a different resulting value in the variables depending on the order of our assignment, because we're asking the computer to take a meaningfully different action each way.
myVar = time() is valid: it says "take the current time value and store it in the variable
time() = myVar is not valid: it says "take the value of
myVar and store it into the current time" which doesn't make sense.
You could argue that programming language authors should have chosen a different symbol/syntax for assignment, like
destination <-- source, but it turns out we need to write assignment operations a lot when we create programs, so having it be short and accessible without a shift key is very useful.
Some languages will let you type just a single
= for both equality comparison and assignment, and the compiler/interpreter will try to figure out which one you wanted based on the surrounding context. This can lead to bugs though if the programmer thought they were comparing but the compiler read it as an assignment, which is why Lua and C-like languages use different operators for them. Some will also warn or error if you write something like this:
if myVar = 5 then...
Which looks like a comparison ("take this branch if and only if
myVar has a value of 5") but is actually an assignment ("set
myVar to 5 and then take this branch"). So some programmers will program defensively by using the fact that flipping an assignment with a constant/function call is an error, to make a what's called a "Yoda Condition":
if 5 == myVar then...
If we remember to use the
== then this comparison works right in either order. If we accidentally forget one
= and end up writing an assignment, it's an error "you can't assign a new value to the number 5!" which helps us find and fix our typo instead of letting the bug linger unfound. ;)