Lua ""==true equals ""==false
All Lua values when used as Booleans evaluate to true, except nil and false. This does not mean that values that evaluate to true are equal to true. If you want to convert a value v
to Boolean, use not not v
.
The type of ""
is string, not boolean, so it's not equal to either true
or false
.
To be more general, when Lua compares two values, it tests their type first, if the type mismatch, Lua thinks the two values as not equal immediately.
When used as control expression, the only false values in Lua are false
and nil
, everything else is evaluated as true value. Some popular confusions include the number 0
, the empty string ""
, the string "0"
, they are all true values. Note again that false
and nil
are not equal because they are different types.
So back to the example, in the code
if "" then -- if ""==true
print "was true"
end
Lua tests if ""
is false
or nil
, since it's neither, then Lua treats the condition as true value.