Data Types
In the previous chapter we've discussed variables, which are named boxes which hold information inside of them.
Now, what can a variable store? We've seen it store numbers (like 20
)
and text (like "Bob"
).
A variable can store much more, the types of things a variable can store is called a data type.
Primitive Data Types
Primitive data types are the most basic kind of data type, any other data type is created using these data types. We will only learn about primitive data types for now.
Number
Pretty self explanetory, a number is any number, it could be an integer, or a number with decimals.
local myFavoriteNumber = 7
local pi = 3.14159265359
You can get the value of pi using math.pi
, we'll learn about the
math library and other libraries in the future.
String
A string is a string of characters, or a list of characters, or simply just text.
local foo = "This is a string!"
print(foo)
Strings start with a quote "
and end with another quote "
, you can also use
single quotes '
. If you want to use a quote inside of a string, like this:
local foo = "The boy said, "Hello!""
This will result in an error, it thinks you are trying to end the string. You can
fix this by escaping the quote, this is done by putting a backslash infront of it \
.
local foo = "The boy said, \"Hello!\""
You could also use single quotes:
local foo = 'The boy said, "Hello!"'
Boolean
A boolean is just true
or false
. This could mean yes or no,
or if a condition is true or false, or if something is positive
or negative. Any value with 2 states can be a boolean.
local foo = true
local bar = false
nil
nil
is a special keyword which represents nothing. This can be used to specify that a value
does not exist. For example, a winner
variable which is nil
if the game resulted in a draw.
local nothing = nil
The typeof()
function
typeof
is a function like print
, it gives you the type of the value you give it.
print(typeof("Foo")) -- prints "string"