Lua Fundamentals

Lesson 2 of 6

Variables, Types, and Control Flow

Lua is dynamically typed, a variable's type is decided by whatever value it currently holds, and can change at runtime.

Basic types

Lua

nil is Lua's "no value", it's what an undeclared variable holds, and it's the only value besides false that counts as falsy in a condition.

Control flow

Lua
  • Every block (if, for, while, functions) is closed with the keyword end, there are no curly braces in Lua.
  • for i = 1, 5 do ... end is a numeric for loop: start, stop, and an optional step (for i = 10, 1, -1 counts down).
  • Only nil and false are falsy, everything else, including 0 and "", is truthy. This is a common gotcha for people coming from JavaScript or Python.

WARNING

Unlike most C-family languages, 0 is truthy in Lua. if 0 then print("yes") end prints "yes". Only nil and false are falsy.

📝 Types & Control Flow Quiz

Passing score: 70%
  1. 1.Which two values are considered "falsy" in a Lua condition?

  2. 2.Lua uses curly braces { } to open and close if/for/while blocks.

  3. 3.Every Lua block (if, for, while, function) must be closed with the keyword ____.