Function toboolean(): Difference between revisions

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 edit summary
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(<u>variable</u>)
  <i>boolean</i> toboolean(<code>variable</code>)


=== Parameters ===
=== Parameters ===
<i>mixed</i> <u>variable</u> will be converted to boolean value.
* <i>mixed</i> <code>variable</code> will be converted to boolean value.


=== Return value ===
=== Return value ===

Revision as of 18:56, 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.