Why Use Luau?
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 2 of 80.
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)
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: 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.
Challenge
EasyGive both parameters of calculateTotal the type number.
- In the first line, change
(price, tax)to(price: number, tax: number). - Keep the call
calculateTotal(80, 8)at the bottom.
Press Run Code: the output shows 88.
Try it yourself
local function calculateTotal(price, tax)
return price + tax
end
print(calculateTotal(80, 8))
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 ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe '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 TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler