PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Jun 27 2019, 3:40 PM PST 10 min

A boolean is a very simple data type with a value of either true or false. Booleans are most commonly used with Articles/Conditional Statements in Lua|conditional statements, for instance:

local testBoolean = true

if testBoolean == true then
	-- Value of 'testBoolean' is true, so this condition is executed
else
	-- If value of 'testBoolean' is false, this condition is executed
end

Lua Evaluation

In Lua, if a value is not false or nil, it’s considered “true” when used in a conditional statement. The code below prints out only the values which Lua interprets as true:

Operators

Based on the evaluation rules above, conditional statements with Lua /articles/Operators|operators work as follows:

And

The and operator returns the first argument if it’s false or nil, otherwise it returns the second argument.

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, then it returns the second value. For instance:

This printed 1 because x doesn’t exist and is therefore nil. Effectively, the or operator let us choose 1 in favor of nil.

This also printed 1 because, although x exists, its value is false. If x had been true, the or operator would have chosen x in favor of 1.

Not

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

Tags:
  • lua
  • value
  • boolean