Free preview.You're sampling one lesson — enroll free to unlock all 7 lessons and track your progress.
Enroll free
lesson

Variables and Logic

Variables and Logic

In this lesson — part of Scripting Basics — you'll learn variables and logic in Lua and why it matters in real work.

Why it matters

Variables are how programs remember values — the foundation of every computation.

Key ideas

  • A variable is a name bound to a value
  • Choose clear, descriptive names
  • Constants vs. reassignable variables
  • Values have types

In practice

Here's how it looks in idiomatic Lua:

-- globals are implicit; 'local' is what you almost always want
local name, age = "Ada", 36   -- multiple assignment
local x = 10
do
  local x = 20               -- shadows outer x in this block
  print(x)                   --> 20
end
print(x)                     --> 10
undeclared = 5               -- silently creates a GLOBAL (a common bug)

Lua note: Any name you don't prefix with local becomes a global living in the _ENV/_G table, so forgetting local is the classic source of accidental shared state.

Try it yourself

Exercise: Declare three variables in Lua (a name, an age, and whether you're learning) and print them.

Recap

You now understand variables and logic and can apply it in Lua. Mark this lesson complete and continue to the next one.