We use cookies on this site to enhance your user experience

Jul 03 2018, 10:15 AM PST 10 min

A Boolean, or Bool for short, is a very simple data type. It is either a true or false value.

Using Booleans

Booleans are most commonly used with Articles/Conditional Statements in Lua|conditional statements.

local myBool = true

if myBool then
	-- If the value of myBool is true, this code is executed!
	-- If the value of myBool is false, this code is executed!


In Lua, if a value is not false or nil then it is considered “truthy”. When used in an if, while, or repeat statement, truthy values are treated as if they were true, and other values are treated as false. The code below prints out only the values which are truthy:

Converting booleans into strings

When converted to a string, booleans will return true or false

If you want to print something other than true or false, you must use Articles/Conditional Statements in Lua|conditional statements.

You can also use the following idiom to get the same results:

print(enabled and "Enabled" or "Disabled")



The not operator returns true if the argument is false or nil, otherwise it will return false.

Value of x Value of not x
true false
false true
nil true
"text" false
0 false
1 false


One thing that the not operator is useful for is toggling something. To make a button toggle the visibility of a GUI, you can do:

    frame.Visible = not frame.Visible


The and operator returns the first argument if it is false or nil, otherwise it will return the second argument.


The or operator operates on two values. If the first value is neither false nor nil, the or operator returns the first value. If the first value is false or nil, then it will return the second value, regardless of what it is.


The or operator comes in handy when you want to check if one of the listed values is a certain value.

Useful Patterns

Choice of value

The or operator can also be used to choose a default value if a value is nil or false. Here are some examples:

This printed 1 because x doesn’t exist and is therefore nil. So the or operator allowed us to choose 1 over nil.

This also printed 1 because although y exists, it is false. If x had been true, then y would be true, because the or operator would choose x, as it is neither false, neither nil.

Conditional expression

A conditional expression sometimes referred to as the ternary operator, is a substitute for a Articles/Conditional Statements in Lua|conditional statement that can be used as part of a larger expression. It allows this:

local x = 100
local msg
if x > 50 then
    msg = "is large"
    msg = "is small"
msg = "your value " .. msg

To be transformed into this functionally equivalent code:

local x = 100
local msg = "your value " .. (x > 50 and "x is large" or "x is small")

It is important to note that for the general case of cond and x or y, the code will not behave as expected if x is not truthy.

For more in-depth discussion of this construct, see TernaryOperator on lua-users.org.

See also


  • coding
  • concept
  • lua