Number, String and Boolean
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 6 of 80.
Luau has three fundamental primitive types that form the building blocks of most programs: string, number and boolean. You've been using these values in Lua all along: Luau lets you declare them as types.
The string type is for text data:
local productName: string = "Laptop"The number type handles all numeric values: integers and decimals alike, just as you know from Lua's single number type:
local price: number = 999.99The boolean type represents true/false values, perfect for flags and conditions:
local inStock: boolean = trueOnce annotated, each variable is held to its type: assign the string "500" to price and the type checker complains immediately. And remember: print(inStock) prints the word true, because booleans stringify to true/false.
Challenge
EasyDescribe a product with three typed variables.
- Declare
local productName: string = "Gaming Mouse". - Declare
local price: number = 49.99andlocal inStock: boolean = true. - Print the three variables, one per line, in that order.
Press Run Code: the output shows Gaming Mouse, 49.99 and true.
Try it yourself
-- 1) declare productName, price and inStock with their types
-- 2) print all three, each on its own line
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