Code

15 px

Ctrl + Enter Run · Ctrl + / Comment

io.read() reads the next line; io.read("n") reads a number.

Ready

Output

Press Run to see the output here.
Saved on this deviceRuns in your browser · no networkLn 1, Col 1Spaces: 2Lua

Online Lua compiler

Write Lua, give it input and run it, in your browser. This is the real Lua 5.4 interpreter, so programs behave exactly as they do with the lua command.

How to use the Lua compiler

Write a program in the editor or pick one from Examples: input and arithmetic, tables as arrays, a prime check, strings, a word count, recursion, objects with metatables, closures, errors with pcall, and sorting with a comparison. Press Run or Ctrl+Enter. print and io.write write to the output; io.stderr:write writes in red.

Your code, input and tests are saved on this device as you type. Share makes a link with the code and input inside it, and Download saves the code as main.lua.

How input works

The Input tab is the program's standard input. io.read() reads the next line without its newline, io.read("n") reads the next number (skipping spaces and line ends), and io.read("a") reads everything left. io.read("n", "n") reads two numbers at once, and for line in io.lines() do goes through the lines that are left. When the input runs out, io.read returns nil, and the compiler notes that the program wanted more input.

What runs your code

Lua 5.4, the official interpreter written in C, compiled to WebAssembly and run in your browser by wasmoon. The whole standard library is there (string, table, math, utf8, coroutine and os.time), except for the parts that need a computer around it: files, os.execute and require of outside modules. Errors are reported the way lua reports them, such as main.lua:3: attempt to perform arithmetic on a nil value.

Each run may take 10 seconds; then it is stopped. Output is kept up to 10,000 lines or 1 MB, and the rest is cut with a notice. Programs cannot reach the network, and no packages or libraries can be installed: what a language brings with it is what you can use. A loop that never ends is stopped at 10 seconds, keeping what it printed.

Questions

Which version of Lua is this?
Lua 5.4, the current version, with integers and floats as separate number types, the // floor division operator and the utf8 library.
How do I read a number?
Use io.read("n"). It skips spaces and line breaks, so "17 5" on one line gives two numbers with io.read("n", "n").
Is there a step-by-step visualizer for Lua?
Not yet: Lua runs on its own interpreter here, which the visualizer cannot follow. Python, C, C++, Java, JavaScript and TypeScript have one.
Does my code leave my computer?
No. It runs in your browser in a sandbox with no network access, and it is saved only on this device.

See also