CodeRigor

FREE LESSON

Values, types & truthiness

Distinguish nil, false and truthy values, keep types explicit, and use local names in a Lua chunk.

Start here; no C++ or other language course is required.

This course runs standard Lua 5.5 with its standard library. Game engines, LuaJIT and embedded host APIs are outside this sequence.

Lua values carry types; variable names do not have a fixed declared type. Start with nil, booleans, numbers, strings, tables and functions. The type function describes a value without converting it. Numbers include integer and floating-point subtypes on this standard interpreter, while type reports number for both. Strings are immutable byte sequences; a later lesson distinguishes patterns and byte counts from Unicode-aware text processing.

A value carries its type
start / 3
A value carries its type

Apply the contract

Only nil and false are false in a condition. Zero and the empty string are both true. The logical operators and/or return operand values, not a normalized Boolean: a and b returns a when a is falsey, otherwise b; a or b returns a when a is truthy, otherwise b. This is useful for defaults, but value or fallback replaces a meaningful false as well as nil. Test nil explicitly when false is valid data.

Exactly two falsey values
start / 3
Exactly two falsey values
Lua
for _, value in ipairs({false, 0, ""}) do
  print(type(value), value and "truthy" or "falsey")
end
print(type(nil), nil and "truthy" or "falsey")

Reason about the edge cases

Prefer local variables so one chunk does not accidentally communicate through globals. The exercise provides local M = {} and return M as module boilerplate; tables and modules receive dedicated lessons later. For now, implement the supplied function body and return a string built with ... Function return values go to the caller; print writes to stdout and is not a substitute. The fixtures include false, nil, zero, an empty string and other value types, so the expected policy is observable without relying on another language's truthiness rules.

Return versus print
start / 3
Return versus print

Practice

Keep the supplied module table and implement M.describe(value). Return truthy:TYPE when the value is true in a Lua condition, otherwise falsey:TYPE; TYPE is the result of type(value). Lua considers only nil and false falsey. Do not print from the module. The grader loads the returned table and calls the function.

Run sample starts again from the first public fixture and grants no mastery evidence. Submit checks all fixtures, including edge cases. Read the specification before editing; the bug and fill phases share the build’s contract. Flashcards reinforce recall on a separate schedule.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

The first part introduces Lua through its language designer. The authored lesson and Lua 5.5 manual cover the exact APIs and advanced details in this lab.