bool||doc136770

September 20, 2022

bool

Bool (Boolean value) is a very simple data type whose value is true or false. Boolean values are often used in conditional statements such as the following code:

local testBool = true

if testBool == true then
    print("do testBool == true condition")
else
    print("do testBool == false condition")
end

In Lua, if a value is not false or nil, it will be regarded as true when used in conditional statements:

if true then
    print("true")
end
if 1 then
    print("1")
end
if "test" then
    print('"test"')
end

if false then
    print("false")
end
if nil then
    print("nil")
end

According to the above estimation rules, conditional statements with Lua operators work as follows:

and

If it is false or nil, the and operator returns the first parameter; otherwise, it returns the second parameter:

print(true and true)
print(true and false)
print(false and true)
print(false and false)
print(4 and 5)
print(nil and 12)
print(false and 12)

or

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, the operator returns the second value. For example:

local x = false
local y = x or 1
print(y)

not

If the parameter is false or nil, the not operator returns true; otherwise, it returns false:

print(not false)
print(not nil)
print(not true)
print(not 1)
print(not "test")