Skip to content

Lua Booleans and Logical Operators Guide — Truthiness and Conditionals

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Lua Booleans and Logical Operators Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Lua booleans represent truth values true and false, but the logical operators and, or, and not do not always return booleans -- they short-circuit and can return any operand, making them useful for default values and conditional assignment.

Boolean Basics

local a = true
local b = false
local c = nil  -- nil is falsy

print(type(true))  --> boolean
print(type(false)) --> boolean
print(type(nil))   --> nil

Logical Operators

-- and: returns first operand if falsy, otherwise second
print(false and "yes")  --> false
print(true and "yes")   --> yes
print(nil and "yes")    --> nil

-- or: returns first operand if truthy, otherwise second
print(false or "default")  --> default
print(true or "default")   --> true
print(nil or "fallback")   --> fallback

-- not: always returns boolean
print(not true)   --> false
print(not false)  --> true
print(not nil)    --> true

Common Idioms

-- Default values
local name = user_input or "Anonymous"

-- Guard clause
user_input and print(user_input)  -- prints only if truthy

-- Ternary-like expression
local max = (a > b) and a or b

Truthiness in Conditionals

-- Only nil and false are falsy
if 0 then print("zero is truthy") end       --> zero is truthy
if "" then print("empty string truthy") end  --> empty string truthy
if {} then print("empty table truthy") end   --> empty table truthy

-- Nil means "no value"
if not value then
    value = "default"
end

Common Mistakes

1. Expecting boolean return from and/or

true and "hello" returns "hello", not true. Wrap in not not or compare explicitly for a boolean.

2. Using and as ternary without caution

(false) and a or b returns b as expected, but (true) and false or b returns b because false is falsy.

3. Confusing nil with false

Both are falsy but not equal: nil ~= false. Use == nil or == false to distinguish.

Practice Questions

1. What does true and false return? False. and returns the first falsy operand, or the last truthy one.

2. What values are falsy in Lua? Only false and nil. Everything else (0, empty string, empty table) is truthy.

3. How do you write a ternary conditional in Lua? condition and value_if_true or value_if_false. But be careful when value_if_true is falsy.

FAQ

{{< faq question="Does Lua have a dedicated boolean type?" >}} Yes, boolean is a separate type with values true and false. Use type(x) == "boolean" to check. {{< /faq >}}

{{< faq question="Can I use 0 or empty string as false?" >}} No. Only nil and false are falsy. 0 and "" are truthy. This differs from C and JavaScript. {{< /faq >}}

{{< faq question="How do I force a boolean result?" >}} Use not not value to convert any value to boolean. not always returns a boolean, and applying it twice gives the truthiness. {{< /faq >}}

What's Next

Now learn about control flow with if statements.

Topic Description Link
Control Flow if, elseif, else {{< ref "17-control-flow" >}}
Loops while and for {{< ref "18-loops" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro