I know the title sounds confusing but let me show what i mean:
local function f(variable)
return not variable or 1 and 3
end
My question is how does the script read this? does it return the equivalent not (variable or 1 and 3) or (not variable) or (1 and 3) or (not variable or 1) and 3?
Im not asking for the outcome of that specific order of statements, Im asking which ones go first in any order
I hope the question is understandable since i really dont know how to clearly explain it
Do note, there is a very slight behavorial difference between the standard Lua idiom (which roughly simulates ternary operators), and Luau’s if-then-else expressions.
Lua idiom:
local a = true
local b = false
local c = "bar"
local foo = a and b or c
print(foo) --> "bar"
This may return an unexpected result if b evaluates to false, as it would then return c instead of b.
Whereas, the Luau expression is more explicit:
local a = true
local b = false
local c = "bar"
local foo = if a then b else c
print(foo) --> false
In most cases, the if-then-else expression is preferred, but sometimes the ‘fallback’ behavior is desired, or it might actually make your code easier to read for simpler expressions as it’s less boilerplate.