0Pricing
Lua Academy · Lesson

Arrays as Tables

1-based indexing in Lua.

Tables Are Lua's Lists

Lua has one container type: the table. When you store values in order, the table acts like an array or list.

You create an empty table with {} and fill it later, or list values right away inside the braces.

local fruits = {"apple", "banana", "cherry"}
print(fruits)

Indexing Starts at 1

This is the most important rule: Lua arrays are 1-based. The first item lives at index 1, not 0.

Use square brackets to read an element by its position.

local fruits = {"apple", "banana", "cherry"}
print(fruits[1])
print(fruits[2])
print(fruits[3])

All lessons in this course

  1. Arrays as Tables
  2. Adding and Removing Items
  3. Iterating with ipairs
  4. Length and Sorting
← Back to Lua Academy