Values and Variables
Contents
In Lua, the commonly used types of values are specified in the table below.
Type | Description | Example |
---|---|---|
nil | Represents the undefined value. nil is not equal to 0 or “” (the empty string). If a variable has not had a value assigned to it, its value is assumed to be nil. | nil |
Boolean | Can be either true or false. | true |
Number | Any number (either containing or not containing a decimal point). In Lua, by default, all numbers are stored as 64-bit floating-point values. | 1198.6 |
String | A string of text characters. See Working With Strings for more details. | “Dr. John Smith” |
Table | A collection of values. See Working With Tables for more details. | {1, 2, 3, 4, 5} |
Function | In Lua, functions can be treated like any other values. | function hello()print(“hello”)end |
Userdata | These are user defined data types from the external application – i.e. node trees in Iguana are userdata objects which internally are implemented in C++. |
Note: There are other value types in Lua, but you are not likely to use them. See the Values and Types section of the Lua reference manual and the online programming book for more details.
Lua is a dynamic language. So a variable can point to any type of value. Variable names consist of letters, digits and underscores. The first character of a variable name must not be a digit. The following are examples of legal variable names:
x text1 myvar MYVAR
In Lua, variable names are case-sensitive: capitalized letters and non-capitalized letters are not treated the same. This means that myvar, Myvar and MYVAR refer to different variables, not the same variable.
In Lua, any variable can contain a value of any type. For example, the following are all legal:
myvar = 98.6 myvar = "Dr. John Smith" myvar = {98.6, "Dr. John Smith", "Radiology"}
For details on how values are assigned to variables, see Assignment and Expressions.
For more detail see: the Values and Types in the online reference manual, the online programming book and the Lua Tutorial wiki.