Why Use Luau?
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 2 of 73.
Luau's type system offers three major benefits that make development safer and more efficient.
Catch errors early: the type checker analyzes your code while you edit, before it runs. Consider a classic Lua bug — passing nil where a number is expected:
-- Lua: nothing warns you about this
local function calculateTotal(price, tax)
return price + tax
end
print(calculateTotal(nil, 0.1))
-- ✗ runtime error: attempt to perform arithmetic on nilIn plain Lua that crash only happens when the bad call finally executes — maybe in front of your users. Worse, Lua sometimes hides the bug entirely: "50" + 0.1 silently coerces the string and returns 50.1… until the day the string isn't numeric. With Luau annotations the mistake is flagged immediately:
local function calculateTotal(price: number, tax: number): number
return price + tax
end
calculateTotal(nil, 0.1) -- ✗ type error while editing: nil is not a numberBetter code readability: annotations serve as documentation. price: number tells you and your teammates exactly what a function expects and returns — no guessing, no digging through the implementation.
Enhanced development tools: because the editor knows each variable's type, you get accurate autocompletion, safer refactors, and smarter navigation. Type a dot after a typed value and the editor lists exactly what's available.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps