First of all, you need to know the meaning of truthy and falsy.
Truthy means that the language considers the value as something that can make an if
statement work - Every value, except false
and nil
, is truthy.
Falsy means that the language considers the value as something that will run the else
part of an if
statement. false
and nil
are falsy.
I will recap the logical operators so it’s easier to see the full picture.
The logical operators work with any value, not just booleans. An and
takes two operands. Normally, we define an and
as “if both of the operands are truthy, return a truthy value”. If the first operand is truthy, then the second must be truthy too, for the and
to return a truthy value. There is an obvious shortcut to take there, that is, to return the second operand itself. The people who implemented Lua did take that shortcut.
If the first operand of and
is falsy, then, the and
should return a falsy value. Lua chooses to return the first operand, as that falsy value to return if the first operand is falsy. It won’t bother evaluating the second operand if the first one is falsy; we call that “short circuiting”.
or
also has similar behavior - or
also returns the last evaluated operand, but the way it short circuits is different. It doesn’t bother evaluating the second operand when the first is truthy, because the result is now confirmed to be a truthy value. If the first operand is falsy, it returns the second operand, which if you think about it, is supposed to be the result of the operation - if the second one is falsy, both are falsy, so return the falsy value.
So now let’s get to the actual question: How does that work?
The assignment isn’t special here, let’s focus on the expression. Let’s have 3 values; x
, y
and z
.
x and y or z
Let’s focus on the and
first. If x
is truthy, evaluate to y
. If it’s not, evaluate to x
. There is an assumption made in this system, and it’s the main flaw of this system. y
should be truthy for this to work. Thankfully it’s not the biggest assumption since the falsy values aren’t usually necessary in an expression like this one.
So, after the and
, we either have the truthy y
value, or if we don’t, we have x
which is now confirmed to be falsy because it short circuited. If we have y
, the or
will short circuit, and we get y
as the result. If we have x
, the or
will return z
.
Therefore, it means: evaluate to y
if x
is truthy, z
if not
What if y
is falsy? Well, Luau, the Lua dialect used by Roblox, has an if expression which can be written as if x then y else z
.
Sorry for creating this reply, I couldn’t see alliedoeihoialt’s reply because of my awesome and totally flawless internet. I kinda overcomplicated the thing as well, when I tried getting into the details, I stopped seeing the full picture I was trying to show.
What’s funny is that when I need to read code, I read or
s as “x, or if it doesn’t exist, then evaluate to y” with “existence” being a synonym I use for truthiness (it’s not a correct synonym though)