OR Operator
In Lua, the logical or
operator is used to set a default value. It's a concise way to say "use this value if it exists, otherwise use this default value.” It is also helpful when a nil
value appears, you can avoid an error and assign a default value
Here is a simple example:
local text = "Hello"
local P = {}
P.text = text or "no text provided"
trace(P.text) -- returns "Hello"
In the above code snippet, since text exists, we will see “Hello“ in P.text Using trace() function.
If we were to comment out line 1 which assigns a value to text, P.text will hold the default value: