Check if nil
📝 LuaChecks 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
Checks whether a variable is nil in Lua to avoid errors when accessing its value.
if var == nil then print("var is nil")
else print("var is not nil")
end
Comments
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.
@leeb sure, but if you're already checking for nil, wrapping it in an 'or false' is a one-word fix for the false case.