Lua doesn't care what type of data a variable has.
data = 12 data = "hello" data = true
To this end, we can use the
type function to check what kind of data a variable is holding.
We can check the type of function:
We can also use it to check what type of data a function is returning back to us:
Converting data types
We've already seen data type conversion previously when we took numbers and an operation in, transforming that into a true or false statement.
type(12 > 3)
There are also ways to convert between numbers and strings using
number = tonumber("24") print(type(number)) string = tostring(number) print(type(string))
Interesting but maybe less useful, you can convert other data types to string:
print(tostring("already a string")) print(tostring(true)) print(tostring(nil)) print(tostring(tostring))
- Which of these strings can be converted to a number successfully?
" 5 ",