After finishing the chapter of the basic Lua Input and Output, we are now moving on to starting out a new chapter about variables and data types. These are the building blocks of any Lua program. If you are new to be a coder, variables allow you to store, manipulate, and manage data.
Few important aspects we are going to address in this chapter:
- What variables are and how to declare them?
- The different types of data Lua works with?
- How Lua handles variables and values dynamically?
I hope at the end, you will be comfortable working with variables and data in Lua editor. Let’s begin!
What is Lua Variable?
A variable is just like a container that holds information. You can think of it as a labeled box where you can store different types of data, such as numbers, text (strings), or even more complex structures like tables.
Definition of Variable
You can use Lua Online Editor to try the examples below:
How to declaring Variables in Lua?
In Lua, create a new variable is really simple, you just need to tell the name of it and usually you will assign a value to the variable by the “=” operator. You can define variables as many as you want. But you need to care about the scopes and types of the variables. Let’s take a look of a simple example:
local name = "KaClass" -- A string variable
local age = 25 -- A number variable
local amISmart = true -- A boolean variable
You can see in this example, we use local keyword to define variables. The first one “name” is a STRING variable which store the text “KaClass”. Then the next one “age” is a number with the value “25”. Finally “amISmart” is a boolean. The only possible values of the boolean variables are true or false.
You may wonder, do we need to define the data type for Lua variable? The answer is NO. Lua scripting is a dynamically typed language. So the type of the variable is determined automatically based on the value assigned.
What is Local and Global Variables in Lua?
In Lua, you can create both local and global variables. Local variables are defined using the local
keyword like what I show you in previous example. They are only accessible within the block or function where they are declared. Just like those variables are “local citizen”, they can only access within the country.
So how about Global variables? We do not use the local
keyword and can be accessed from anywhere in the program. Just imagine you have a global VISA which allow you travel around the world! Let’s take a look of below example:
Local vs. Global Variables
local localVar = "I am local citizen"
globalVar = "I got a global visa!"
print(localVar) -- Output: I am local citizen
print(globalVar) -- Output: I got a global visa!
So why we don’t set all the variables to be global? Because using local
scope helps avoid conflicts during programming. It can makes your code more manageable by limiting the scope of variables.
Data Types in Lua
As mentioned previously, Lua is a dynamically typed language in which you don’t need to explicitly define the data type of a variable. So how does it work? The engine of Lua will figures it out based on the value you assign to it. However, it’s still important to understand the basic data types Lua supports such that you can have better control of the data flow.
Type: Nil
This is a special type that represents the absence of a value. Usually in programming, you need to take special care of whether the variables are NULL. In general, variables haven’t been assigned any value intially automatically hold the nil
value. We can try it out now.
Printing Nil example
local randomVar
print(randomVar)
In this case, randomVar
is declared but not initialized. So it holds the value of nil
.
Type: Boolean
In some situation, we need to assign a variable either TRUE or FALSE. We use Booleans to handle. Lua uses true
and false
as boolean values. They’re typically used in conditional checks.
Printing Boolean example
local isGameOver = false
print(isGameOver)
Type: Number
It’s really common to create number variable. Lua has a single type for all numbers, which can represent both integers and floating-point numbers (decimals).
Printing Numbers in Lua example
local count = 10 -- An integer
local price = 5.99 -- A floating-point number
print(count) -- Output: 10
print(price) -- Output: 5.99
Numbers in Lua are handled efficiently, and you can perform all basic arithmetic operations with them, such as addition, subtraction, multiplication, and division.
Type: String
Strings are another common data type you will frequently used. It literally used to store text in Lua. The syntax of defining string can be enclosed in single quotes ('
), double quotes ("
), or even double square brackets ([[ ]]
) for multiline strings.
Printing Strings in Lua
local greeting = "Hello, World!"
local longText = [[
This is a long string
that spans multiple lines.
]]
print(greeting) -- Output: Hello, World!
print(longText) -- Output: The entire multiline text
We will discuss in a bit later chapter about how many useful functions offered by Lua so that you can manipulating them easily. For instance, concatenation, length checking, and substring extraction are commonly used in coding. See for example:
How to string concatenation in Lua?
You can join two or more strings using the concatenation operator ..
local firstName = "John"
local lastName = "Wick"
local fullName = firstName .. " " .. lastName .. "is Cool!"
print(fullName) -- Output: John Wick is Cool!
Type: Table
Tables are one of the most powerful and flexible data types in Lua. By using table, you can represent arrays, dictionaries (key-value pairs), and objects. We will talk more about table with examples in detail in another chapter, but for now, just know that tables are incredibly versatile.
How to create table in Lua?
local person = {
name = "Taylor Swift",
age = 35,
job = "Singer"
}
print(person.name) -- Output: Taylor Swift
Tables can store all kinds of data, including numbers, strings, booleans, and even other tables.
Dynamic Typing in Lua
Lua is dynamically typed, you can change the type of a variable simply by assigning it a new value. By doing that, the engine of Lua will automatically adapts to the new type of the value you assign.
How Dynamic Typing in Lua work?
local myVar = 1000 -- Initially a number
print(myVar) -- Output: 1000
myVar = "I am loving it" -- Now it's a string
print(myVar) -- Output: I am loving it
In above example, myVar
starts as a number and then becomes a string. You can see that Lua doesn’t require you to specify the type in advance.
How to check the type of the variable?
In some circumstance, you might want to check the type of a variable. By using the type()
function, you can do it and it’s helpful for debugging or validating the data types of variables.
Checking type in Lua example
local number = 55
local text = "Beauty of Art"
local isTrue = false
print(type(number)) -- Output: number
print(type(text)) -- Output: string
print(type(isTrue)) -- Output: boolean
The type()
function returns a string that describes the type of the variable.
Summary of Lua Data Types
Here’s a quick overview of the basic data types we have discussed so far:
Data Type | Description |
---|---|
nil | Represents the absence of a value. |
boolean | Holds either true or false . |
number | Stores both integers and floating-point. |
string | Holds sequences of characters or text. |
table | Lua’s flexible data structure. |
function | Functions can be assigned to variables. |
Conclusion
I hope you have a solid understanding with variables and data type after finishing this chapter. By understanding how to declare variables and work with different types of data, you will be able to face different challenges coming. Just make sure you remember the dynamic type property of Lua. Try experimenting more with your own Lua editor to get familiar with all of them. In the next chapter, we will start explore the Lua operators. See you soon!