Menu
Coddy logo textTech

What is a Variable?

Part of the Fundamentals section of Coddy's Lua journey — lesson 4 of 90.

Variables: Think of a variable as a labeled container that can hold information. Just like you might put items in a box and write a label on it to remember what's inside, variables let you store data and give it a meaningful name so you can use it later.

In Lua, creating a variable is simple. You write the name you want to give it, followed by an equals sign, and then the value you want to store:

playerScore = 100

This creates a variable called playerScore and stores the number 100 in it. Now whenever you need to use that score in your program, you can simply refer to it by name instead of typing 100 every time.

To display a value on the screen, use the print() function. Place the variable name inside the parentheses and Lua will output its value:

playerScore = 100
print(playerScore)

This will output 100 to the screen. Notice that when printing a variable, you write its name without quotes — quotes are used for text (strings), not for variable names.

challenge icon

Challenge

Easy

Create a variable called gameLevel and assign it the number 5. Then print the value of this variable.

Cheat sheet

Variables are labeled containers that store information. In Lua, create a variable by writing its name, followed by an equals sign, and then the value:

playerScore = 100

You can then use the variable name to refer to its stored value throughout your program.

Use print() to display a value in the output. Pass the variable name inside the parentheses to print its stored value:

print(playerScore)  -- outputs: 100

Try it yourself

-- TODO: Write your code here
quiz iconTest yourself

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

All lessons in Fundamentals