Skip to content

What is Lua?

Updated Nov 30, 20263 min readBased on official documentation
What is Lua?

In short

Lua is a small, lightweight programming language designed to be embedded inside other programs. It is used to add scripting, settings and game logic to larger applications. Lua is known for being simple, fast, and easy to combine with code written in C.

What Lua is

Lua is a programming language. It was created in Brazil and is now maintained as an open project. Its main goal is to be small and easy to add to other software. Many programmers use Lua not as a stand-alone tool but as a scripting layer inside a larger application. A scripting language is a language you write short programs in, and a host program runs those programs while it works.

Lua's official reference manual describes the syntax of the language, its meaning, its standard libraries, and its C API. The C API is the set of functions a C program uses to talk to Lua. If you want a gentle introduction to writing Lua code, the book Programming in Lua is recommended by the Lua project itself.

How it works

Lua code is usually run by the Lua interpreter, a program that reads your source and executes it. Lua is often delivered as a small library, so a host application can start a Lua state, load a script, and call functions in it. The language has a few core data types, including numbers, strings, booleans, tables, and functions. Tables are the main way to organize data in Lua. They act as arrays, dictionaries, or objects, depending on how you use them.

Lua also supports first-class functions. This means you can store a function in a variable, pass it to another function, or return it from a function. Lua uses lexical scoping, so a function can see the variables that were visible where it was written.

A small example shows the general feel of the language:

local player = { name = "Mira", score = 0 }

function addPoints(p, amount)
  p.score = p.score + amount
end

addPoints(player, 10)
print(player.name, player.score)

The local keyword creates a variable that exists only in the current block. The table holds two fields, and the function changes one of them. Notice that Lua does not require semicolons and does not force you to write classes.

Versions of Lua

Lua has several released versions, and the official site keeps manuals for each one. At the time of writing, the manual for the latest version is available in English only. The manual for Lua 5.4 is also in English only. The manual for Lua 5.3 is available in English and Russian, and the manual for Lua 5.2 is available in English, Portuguese, and Polish. Manuals for Lua 5.1 and older versions are also provided. The official site also offers offline copies of the Lua 5 manuals in its download area.

When you start a new project, read the manual for the version you plan to use. Code written for one version may behave slightly differently in another. Check the version your host application expects before you write anything.

When to use it

Lua fits well when you need a language that is easy to embed. Game engines, embedded devices, and network tools often use Lua for configuration and scripting. Because the language core is small, it does not take much memory or space in your program. It also makes it easy to let users extend an application without giving them access to the whole codebase.

Lua is less suited to large standalone applications where you need a huge ecosystem of ready-made libraries. In that case, a general-purpose language may be a better fit. If you are new to scripting in web pages, you might first read our guide to What is JavaScript?, which covers a different kind of scripting language.

Common mistakes

Beginners often expect Lua to behave like a web language. It does not run in a browser by default, so you need a host program to run it. Another common mistake is forgetting that arrays in Lua start at index 1, not 0. Many standard operations on tables assume this convention, so keep it in mind when you loop over data.

Also be careful with global variables. If you do not write local in front of a variable, it becomes global and can be changed by any code that runs later. Using local variables by default keeps your scripts easier to reason about and test.

Alternatives

Several other small languages serve similar needs. Some teams choose a language with a different design, such as What is Zig? for systems work or What is Clojure? for functional programming on the Java platform. The right choice depends on your host program, your team, and the kind of work you do. Lua remains a strong option when you want a tiny, well-documented language to hand over to your users.

FAQ

Is Lua hard to learn?

No. Lua has a small set of core features and a simple syntax. Many developers can write useful scripts within a few days of reading an introduction.

Can Lua run in a web browser?

Not by itself. Lua usually runs inside a host program written in another language, such as C. Browser scripting is normally done with JavaScript.

Why do people embed Lua in other programs?

Lua is small and fast to start, so it adds little weight to a larger application. It lets users or developers change behavior without rebuilding the whole program.

Where can I find the official Lua manual?

The reference manual is published on the official Lua website at lua.org. Manuals exist for several versions, so choose the one that matches your setup.

Sources

This page is based on the official documentation below. Read it for full detail.

Lua — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.