The type() Function
Part of the Fundamentals section of Coddy's Lua journey — lesson 9 of 90.
The type() function takes any value or variable as input and returns a string that tells you what data type it is. Here's how it works:
playerScore = 100
playerName = "Coddy"
isReady = true
emptyValue = nil
print(type(playerScore)) -- outputs: number
print(type(playerName)) -- outputs: string
print(type(isReady)) -- outputs: boolean
print(type(emptyValue)) -- outputs: nilChallenge
EasyYou are provided with several pre-defined variables containing different types of data. Use the type() function to determine and print the data type of each variable.
The variables are:
mysteryValue1 = 42mysteryValue2 = "Adventure"mysteryValue3 = falsemysteryValue4 = nil
Print the type of each variable in order, one per line.
Expected Output:
number
string
boolean
nilCheat sheet
The type() function returns the data type of a value or variable as a string:
playerScore = 100
playerName = "Coddy"
isReady = true
emptyValue = nil
print(type(playerScore)) -- outputs: number
print(type(playerName)) -- outputs: string
print(type(isReady)) -- outputs: boolean
print(type(emptyValue)) -- outputs: nilTry it yourself
-- Pre-defined variables
mysteryValue1 = 42
mysteryValue2 = "Adventure"
mysteryValue3 = false
mysteryValue4 = nil
-- TODO: Write your code here to determine and print the type of each variableThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator