What are Lua Variables?
In this article, we’ll take a closer look at Lua variables, how they work, and some best practices for using them effectively.
One of the core features of Lua is its support for variables, which allow programmers to store and manipulate data throughout their programs.
It goes without saying that variables play a vital role in programming in any language, and Lua is no different.
At its core, Lua variable is a container for storing a value. This value can be a number, a string, a boolean, or any other Lua data type.
Lua variable value types will be discussed in the next article.
Lua Variable Types
Lua variables doesn’t have data types, instead it has three types based on variable scope:
|Global variables||It is assumed that all variables are global unless they are explicitly declared as local. Global variables can be accessed from anywhere in the program.|
|Local variables||When a variable is specified as local, its scope is limited to the functions within its scope. A local variable can have the same name as a global variable, but they are treated as two different variables.|
|Table fields||Variables with this type can hold any value except nil, including other tables and functions. Table fields can be accessed using the dot notation or the bracket notation.|
Variable names can be composed of letters, digits, and underscores. The name must either begin with a letter or with an underscore.
Due to Lua’s case-sensitive nature, uppercase and lowercase letters are distinguished.
Several reserved keywords in Lua cannot be used as identifiers, including “if“, “else“, “while“, and “function“.
Lua Variable Definitions
Lua Variable definition refers to the act of specifying the type and scope of a variable.
In Lua, variables do not have an explicit type, so variable definition typically involves specifying the scope of the variable using the local keyword.
-- variable definition local _var = "hello Developers"
Here _var is both declared and defined as a local variable. This means it is only accessible within the block of code where it is defined, and it cannot be accessed from outside that block.
Creating a variable and assigning it an initial value is known as variable declaration.
When using the variableName = initialValue syntax, variableName stands for the variable name and initialValue stands for the initial value.
-- variable declaration and initialization _var = 36
In above code _var is declared and initialized with a value of 36. This variable is not defined as local, so it can be accessed anywhere in the program.
Assign Multiple Variables
The assignment for multiple variables follows a variable_list and value_list format.
type variable_list = value_list;
Below examples demonstrate the declaration of multiple variables:
local m,r,x = 3,6,9;
The above code snippet declares the variables m, r and x as local variables.
Here, the value of m is explicitly set to 3, r takes on the value of 6 while x is assigned the value of 9.
m,r,x = 3,6,9;
Above code is used to declare m, r and x variables as global variables. The value of m is specified as 3, r inherits the value of 6 while x is assigned the value of 9.
m,r,x = 3,6;
And above code example is also used to declare m, r and x variables as global variables. The value of m is defined as 3, r is assigned the value of 6 while the value of x is set to nil.
Below example is used to illustrate the concepts of variable definition and declaration in Lua:
Left And Right Values
In Lua, expressions are categorized into two types:
|Lvalues||Expressions that point to a particular memory location are known as lvalue expressions. These expressions can appear on either the left-hand or right-hand side of an assignment.|
|Rvalues||Data values stored at a particular memory address are referred to as rvalues. Values cannot be assigned to these expressions and they can only appear on the right-hand side of an assignment.|
Variables are lvalues and can appear on the left-hand side of an assignment. On the other hand, numeric literals are rvalues and cannot be assigned a value or appear on the left-hand side of an assignment.
For example, the statement x = 10 is valid because the lvalue x is being assigned the rvalue 10.
However, the statement 10 = 50 is invalid and will generate a build-time error since 10 is an rvalue and cannot be assigned a value.
In Lua, it is also possible to assign multiple lvalues and rvalues in a single statement. For instance, m,r = 12,15 assigns 15 to m and 15 to r.
If you find Lua Variables useful, we would appreciate it if you could share it with others on social media. You can easily do so by clicking on the share buttons provided below.