syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
Can Lua conditionals branch on non-booleans?

Lua allows me to use variables of different types in if-then conditionals where a boolean might be expected.

I usually use this behavior to check for the existence of a variable:
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $ lua
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> myvar = 2
> if myvar then print('myvar is here') end
myvar is here
>
> if yourvar then print('yourvar is here') else print('yourvar is nil') end
yourvar is nil
>
> os.exit()
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me