Menu
Coddy logo textTech

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: nil
challenge icon

Challenge

Easy

You 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 = 42
  • mysteryValue2 = "Adventure"
  • mysteryValue3 = false
  • mysteryValue4 = nil

Print the type of each variable in order, one per line.

Expected Output:

number
string
boolean
nil

Cheat 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: nil

Try 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 variable
quiz iconTest yourself

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

All lessons in Fundamentals