Lua performs automatic type conversions between numbers and strings. Strings will be converted into a number data type if used with arithmetic operators. Whereas numbers will be converted into a string data type if used with concatenation.
-- Conversion of a number type to string typeprint("The number value " .. 5 .. " will be converted into a string")-- Output: The number value 5 will be converted into a string-- Conversion of a string type to number typeprint("1" + 1)-- Output: 2
tostring()
and tonumber()
The built-in functions tostring()
and tonumber()
can explicitly convert data types. tostring()
can convert any data type into a string type. Likewise, tonumber()
can convert any data type into a number type.
In the example, the value 3
in variable b
is converted into a string type. While the value "2"
in variable a
is converted into a number type.
a = "2"b = 3print("There are " .. tostring(b) .. " bees")-- Output: There are 3 beesprint(tonumber(a) + 1)-- Output: 3
Variables are used to maintain code-reusability by storing, retrieving, and manipulating data. Any data stored inside of a variable can be used throughout the program.
-- Declaring a variable with a number valuenumOfSlices = 8-- Accessing the variable and reusing itprint("This pizza has " .. numOfSlices .. " slices!")print(numOfSlices .. " is the best number!")-- Output:-- This pizza has 8 slices!-- 8 is the best number!
Variables are declared with a name and are assigned a value using an equal sign (=
).
userName = "flap_jacks"
Variables can contain values of any data type such as string
, number
, boolean
, and nil
.
-- A variable storing a number typesomeNumber = 8-- A variable storing a string typesomeString = "Anteater"-- A variable storing a boolean typesomeBoolean = true-- A variable storing a nil typesomeNil = nil
type()
The type()
function returns a string with the data type of the argument passed to it.
a = 10print(type(a)) -- Output: numbera = "croissant"print(type(a)) -- Output: stringa = trueprint(type(a)) -- Output: booleana = nilprint(type(a)) -- Output: nil
Multiple values can be combined together to produce a single string using the concatenation operator (..
). The concatenation operator can work with both string and number data types.
num = 10location = "Blockbuster"print("There are " .. num .. " kids in " .. location )-- Output: There are 10 kids in Blockbuster
Arithmetic operators are used to modify numerical values. The arithmetic operators supported in Lua are:
+
) -
) *
)/
)^
)%
)-
)firstNumber = 10secondNumber = 5-- Additionprint(firstNumber + secondNumber) -- Output: 15-- Subtractionprint(firstNumber - secondNumber) -- Output: 5-- Multiplicationprint(firstNumber * secondNumber) -- Output: 50-- Divisionprint(firstNumber / secondNumber) -- Output: 2-- Exponentiationprint(firstNumber ^ secondNumber) -- Output: 100000-- Remainderprint(firstNumber % secondNumber) -- Output: 0-- Negationprint(-firstNumber) -- Output: -10
Variables can store values of any data type (number
, string
, boolean
, and nil
). After a value is assigned, the variable can be updated to a new value as needed.
-- Initial assignmentuserName = "flap_jacks"-- ReassignmentuserName = "pancakes"
Data is information stored by a computer.
-- The following are all example of data991.3"a FuNKY sTriNg""A longer, more formal, less funky string"falsenil
Data always has a data type that determine what operations can be performed on the data. Basic data types are number, string, boolean, and nil.
-- An assortment of data with different data types:11 -- Number"Hello!" -- Stringfalse -- Booleannil -- Nil
type()
The type()
function returns a string with the data type of the argument passed to it.
print(type("Candied Apples")) -- Output: stringprint(type(45)) -- Output: number
The boolean data type can be either true or false.
true -- Note that boolean values do not have single or double quotes wrapped around itfalse
The number data type is any numeric value including integers and decimals.
10113.56
The string data type is any sequence of zero or more characters surrounded by single quotes '
or double quotes "
.
'Hello, world!'"Goodnight, universe!"
The nil data type represents the absence of a value.
nil
Arithmetic operators are used to transform number data, the arithmetic operators supported are:
+
) -
)*
) /
) ^
) %
) -
)-- Addition10 + 2-- Subtraction90-23-- Multiplication3 * 4-- Division64 / 8-- Exponentiation4 ^ 2-- Remainder16 % 2-- Negation-5
An expression is any code that resolves to a value.
1 + 1 -- Expression resolves to the value 2.