Difference between revisions of "Function toboolean()"

From wiki.netio-products.com
Jump to navigation Jump to search
(Založena nová stránka s textem „{{DISPLAYTITLE:function toboolean()}} Converts variable to boolean (true or false) value (the only type usable in conditions). <i>boolean</i> toboolean(<…“)
(No difference)

Revision as of 19:36, 18 April 2017

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

boolean toboolean(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.