Chapter I · Language basics / Lesson 3 of 27

Truthiness & and/or

You'll learn: Use Lua's truthiness rule and the operand-returning behaviour of and/or.

Only nil and false are falsy. 0, "", and even {} are all truthy — which surprises everyone. and/or don't return booleans; they return whichever *operand* decided the result. x or default is the canonical default-value idiom; (cond and a) or b is Lua's ternary expression.

truthiness.lua
Output idle
Hit Run to execute.
Try it: Make (false and "a") or "b" return "b". Now make it return false when the first branch is false — why is (c and a) or b unsafe when a can be false?