function toboolean()

From wiki.netio-products.com
Jump to navigation Jump to search

Converts variable to boolean (true or false) value (the only type usable in conditions).

boolean toboolean(mixed variable)

Parameters

  • mixed variable will be converted to boolean value.

Return value

Falsy values are:

  • number 0
  • boolean false
  • string ""
  • table {}

Anything else converts to true.

Usage

local answer = 42
if toboolean(answer) then
  log("42 is true!")
else
  log("42 is false!")
end

To use inverse conversion (boolean to number) use 'x and 1 or 0' idiom:

local x = true
logf("x is %d", x and 1 or 0)

See also

Lua system's tonumber() and tostring() functions.