The Value 'nil'
Part of the Fundamentals section of Coddy's Lua journey — lesson 8 of 90.
nil. This special value represents the absence of any value - think of it as an empty container.
In Lua, when you declare a variable but don't assign anything to it, that variable automatically contains nil:
local emptyVariable
print(emptyVariable) -- This will output: nilThe keyword local is used to declare a variable that belongs to the current scope — meaning it's only accessible within the block of code where it's defined. It's the recommended way to create variables in Lua.
The value nil is Lua's way of saying "nothing is here." It's different from the number 0, an empty string, or the boolean false - it literally means no value has been assigned yet.
Challenge
EasyCreate a variable called playerStatus without assigning any value to it. Then print this variable to see what Lua assigns by default.
Cheat sheet
nil represents the absence of any value in Lua. Declaring a variable without assigning a value automatically sets it to nil:
local emptyVariable
print(emptyVariable) -- nilUse local to declare variables scoped to the current block. nil is distinct from 0, "", or false — it means no value has been assigned.
Try 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