This article is part of a larger series on the Lua Basics.
- Please check out the Lua Basics if you haven't already.
- For more documents within this series, please scroll to the bottom of the page or click here.
Values are essentially data within Lua. Values can be passed into functions as arguments and can be stored in variables for future use.
There are several different types of data within Lua. We'll go over some of the more basic ones below.
boolean
Booleans store either a true or false value, nothing else. They can be operated on with logical operators such as and
, or
, and not
.
local a = true
local b = false
print(a and b) --> false
print(a or b) --> true
print(not a) --> false
print(a and not b) --> true
number
Numbers store any numeric value, including integers and floats. Numbers are primarily operated on using arithmetic (+
, *
, %
, etc.) and relational (==
, >
, <=
, etc.) operators.
local a = 3
local b = 5.5
local c = 9
print(a + b) --> 7.5
print(b * c) --> 49.5
print(c / a) --> 3
string
Strings are sequences of characters usually made to form human-readable text. They can be created by wrapping characters within quotes ("
) or apostrophes/single quotes ('
).
The character used to wrap the string, either "
or '
, cannot be normally used within the string without a syntax error. These characters can be escaped by putting a backslash (\
) behind them to use them as normal. Newlines are similar, however they are escaped by using \n
.
Literal strings, wrapped using double brackets ([[example]]
), automatically escape all characters within, including newlines. This means that literal strings can go over multiple lines, be represented as such in the value, and still not cause a syntax error.
Strings can be manipulated using the string library, accessed either by using the string
global variable or by using the methods of a string object.