Printing Variables
Part of the Fundamentals section of Coddy's Lua journey — lesson 27 of 90.
The values stored in variables. When you pass a variable name to print(), it outputs whatever value is currently stored in that variable.
Here's how to print a variable's value:
playerName = "Alex"
print(playerName) -- Output: Alex
score = 150
print(score) -- Output: 150Notice that when printing variables, you don't use quotes around the variable name. The print() function automatically retrieves the current value from the variable and displays it. This works with any data type - strings, numbers, booleans, or even nil.
isReady = true
print(isReady) -- Output: true
emptyValue = nil
print(emptyValue) -- Output: nilChallenge
EasyCreate a variable called characterName and assign it the value "Warrior". Create another variable called currentLevel and assign it the value 12. Print both variables to display the character's name and level.
Cheat sheet
To print a variable's value, pass the variable name to print() without quotes:
playerName = "Alex"
print(playerName) -- Output: Alex
score = 150
print(score) -- Output: 150This works with any data type:
isReady = true
print(isReady) -- Output: true
emptyValue = nil
print(emptyValue) -- Output: nilTry it yourself
-- TODO: Write your code hereThis 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