Menu
Coddy logo textTech

Typed Arrays

Part of the Introduction To Luau section of Coddy's Lua journey — lesson 13 of 73.

Typed arrays are one of Luau's most useful features for organizing multiple values while keeping type safety.

In Lua, an array is just a table with values at positions 1, 2, 3, … — and it will happily hold a number, a string and a boolean all at once. Luau lets you declare that a table should only contain elements of one type, using the {Type} syntax:

local numbers: {number} = {1, 2, 3, 4, 5}
local names: {string} = {"Alice", "Bob", "Charlie"}
local flags: {boolean} = {true, false, true}

When you declare a typed array, Luau's type checker enforces that only values of the declared type go in. Insert a string into a {number} array and you get a type error while editing — before the code ever runs:

local scores: {number} = {85, 90, 78}
table.insert(scores, "oops") -- ✗ type error: string is not a number

Everything you already know about Lua tables still applies: arrays are 1-based (scores[1] is the first element), #scores is the length, and table.insert / table.remove work as usual. The annotation adds safety, not new runtime behavior.

challenge icon

Challenge

Easy

Create a typed array named scores that can only hold numbers, initialized with 85, 92 and 78.

Create a second typed array named studentNames that can only hold strings, initialized with "Alice", "Bob" and "Charlie".

Then print, each on its own line:

  1. the first score
  2. how many scores there are (use #)
  3. the second student name

Try it yourself

-- Write code here
-- 1) declare scores: {number} and studentNames: {string}
-- 2) print first score, the scores count, and the second name
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To Luau