Menu
Coddy logo textTech

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

The 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 icon

Challenge

Easy

Create 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)  -- nil

Use 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 here
quiz iconTest yourself

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

All lessons in Fundamentals