Difference between revisions of "Function toboolean()"

From wiki.netio-products.com
Jump to navigation Jump to search
 
Line 2: Line 2:
 
Converts variable to boolean (true or false) value (the only type usable in conditions).
 
Converts variable to boolean (true or false) value (the only type usable in conditions).
  
  <i>boolean</i> toboolean(<code>variable</code>)
+
  <i>boolean</i> toboolean(<i>mixed</i> <code>variable</code>)
  
 
=== Parameters ===
 
=== Parameters ===

Latest revision as of 18:22, 5 May 2017

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.