← Back to Gists

Check if nil

📝 Lua
michaelsimmons
michaelsimmons · 4d ago
Checks whether a variable is nil in Lua to avoid errors when accessing its value.
Lua
if var == nil then print("var is nil")
else print("var is not nil")
end

Comments

-1
leeb leeb 1d ago
yeah @aellis, that's one of those things that trips up people moving from other languages where nil is handled differently. i'd add that sometimes you want to check for false too, since in lua false and nil are distinct but both can cause issues in conditionals.