{}const=>[]async()letfn</>var
DevelopmentLua

Variables and types in Lua: how they differ from Python and JS

Let's take a look at how variables and types are arranged in Lua, and how they differ from the usual Python and JavaScript.

К

Kodik

Author

3 min read

When you start working with Lua, one of the first questions is how are the variables and types arranged here? Especially if you've already written in Python or JavaScript, where everything seems to be "dynamic" too, but there are nuances. Let's figure it out.

Default variables in Lua global. This is a big difference from Python and JavaScript, where everything you create inside a function is local.

x = 10      -- глобальная переменная
local y = 5 -- локальная переменная

👉 It is a good practice to always use local to avoid cluttering the global namespace and catching unexpected bugs.

Types in Lua: 8 basic

  • nil

  • boolean

  • number

  • string

  • function

  • userdata

  • thread

  • table

local a = 42        -- number
local b = "hello"   -- string
local c = true      -- boolean
local d = nil       -- nil

📌 Unlike Python, where numbers are divided by int, float, complex, in Lua it's all just number. It simplifies life, but sometimes reduces control.

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

Dynamic typing: like in JS and Python?

Yes, Lua, like Python/JavaScript, is a language with dynamic typing. The variable can change type on the fly:

local v = 10
print(type(v))  -- number

v = "line"
print(type(v))  -- string
# Python
x = 10
x = "line"
// JavaScript
let x = 10
x = "line"

But there is a subtle point: Lua has no built-in mechanisms for type restriction (unlike TypeScript, mypy, etc.). Everything depends on the programmer's discipline.

Tables: main universal type

If Python has lists, dictionaries, sets, and in JS — objects and arrays, then in Lua all this is combined into one type — table.

local arr = {1, 2, 3}       -- как массив
local dict = {x=10, y=20}   -- как словарь

👉 This is the power of Lua: fewer types — more flexibility. But also more responsibility 😅.

How is Lua different from Python and JS?

Characteristics

Lua

Python

JavaScript

Default variables

Global

Local in function

Local in function (let/const)

Number of built-in types

8

14+

7+

Number type

One (number)

int, float, complex

number, bigint

Main data structure

table

list, dict, set, tuple

object, array

Type system

Dynamic, unlimited

Dynamic but strict

Dynamic, with "magic" (NaN, etc.)

Lua is a language with a simple and flexible data model. It is not overloaded with types like Python, and it is not as "strange" with numbers as JavaScript. But because of global variables and the lack of strict typing, you need to be especially careful.

📌 Therefore, if you want to write in Lua without pain — always put local and keep a close eye on what is stored in the variables.

In Codice we make programming training fun and easy to understand: we have interesting courses with tasks that help you improve your skills step by step.

And we also have an active Telegram channel, where we discuss cool ideas, share experiences and analyze tasks together — learning becomes not only useful, but also fun.

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card