Lesson 06 of 55
9 mins readPython Booleans, Nil & Truth Values
In Plain English
In Lua, **only `false` and `nil` evaluate to false**. Crucially, the number `0` and the empty string `""` are truthy in Lua! Logic operators are `and`, `or`, and `not`.
Deep Dive: How It Works
The Only Two Falsy Values: `false` and `nil`. Everything else (including `0` and `""`) is TRUE.
Short-Circuit `and`: Returns its first argument if false/nil; otherwise returns its second argument.
Short-Circuit `or`: Returns its first argument if truthy; otherwise returns its second argument.
Ternary Idiom: `condition and val_if_true or val_if_false` (requires val_if_true to not be false/nil).
Core Rules to Remember

0 and "" are TRUTHY: Major difference from C/JS/Python: 0 is completely true in Lua boolean conditions.

Default Value Idiom: `x = x or "default"` is standard Lua idiom for defaults.
Live Interactive Example
Hit Run Code to see it liveLua Truth Values and Idioms
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: 0 and "" both evaluated to true. or provided default 8080.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Implement Default Fallback with or
Declare `local user_name = nil`.
Set `local display_name = user_name or "Guest"`.
Print `"Welcome: " .. display_name`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.