What is the proper way to make a conditional which checks of something is or is not empty in Lua? if x == ""
and f x ~= ""
does not seem to work.
The IS NULL operator is used to test for empty values (NULL values).
When programming in Lua, it is important to understand the different meanings of nil and NULL. Nil is the "unknown type" in the context of Lua whereas null and NULL represent the SQL NULL.
In Lua both nil and the boolean value false represent false in a logical expression. Anything that is not false (either nil or false ) is true .
Lua is a dynamically type-based language.
Any variable can hold one of the following types: nil, boolean, number, string, table, function, thread, or userdata.
Any variable in a table (including _G
, the table where globals reside) without a value gives a value of nil
when indexed. When you set a table variable to nil
, it essentially "undeclares" it (removing the entry from memory entirely).
When a local
variable is declared, if it is not assigned immediately it is given a value of nil
. Unlike table variable, when you set a local
variable to nil
, it does not "undeclare" it (it just has a value of nil
).
In Lua, an empty string (""
) is still a "value" - it's simply a string of size zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With