Assign this or that in Lua

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Lua

In Lua, the and and or operators can be used to assign a value to a variable based on a certain condition.

The or operator works by returning the first value that is not nil or false. If all values are nil or false, it returns the last value.

Example:

local x = nil or 42    -- x will be assigned the value 42, as nil is false
local y = false or 43  -- y will be assigned the value 43, as false is false
local z = "foo" or "bar"-- z will be assigned the value "foo", as "foo" is not nil or false

The and operator works by returning the first nil or false value. If all values are not nil or false, it returns the last value.

Example:

local a = nil and 13                 -- a will be assigned the value nil, as nil is false
local b = false and 23               -- b will be assigned the value false, as false is false
local c = "foo" and "bar"            -- c will be assigned the value "bar", as "foo" is not nil or false
local d = "foo" and "bar" and "baz"  -- d will be assigned the value "baz", as all values are not nil or false

It is important to note that the or and and operators only return the value, they don't modify the variable itself. The variable must be assigned the value explicitly, for example:

local x = nil
if x == nil or x == false then
  x = 42     -- x will be assigned the value 42
end