Skip to main content

Nil values

nil is a special value that is used to represent nothingness or non-existence — this is useful for destroying variables, or removing entries from a table.

local a = 1

a = nil <-- Assigning nil to "a" effectively destroys the variable

local b = {}
b[1] = 'hello'
b[2] = 'world'
b[3] = '!'

b[3] = nil <-- Assigning nil to b[3] effectively removes it from the table

print(#b) <-- Since b[3] is now nil, the length of the table will be 2

Note that #table will return the length of a table. So print(#b) will return the length of the table "b".