Lua Variables - Lua (programming language)

How many types of variables are included in Lua?

Variable is a name given to a storage area which our programs can manipulate. It can hold different types of values including functions and tables.

Variable name is a collection of letters, digits and underscore character. It should begin with either a letter or an underscore. Upper and lowercase letters are different as Lua is case-sensitive. Lua has eight basic types of values

Lua does not have variable data types and there are only three types based on the scope of the variable.

  • Global variables − All variables are considered as global unless they are explicitly declared as local.
  • Local variables − When a variable type is mentioned as local, then its scope will be limited with the functions inside their scope.
  • Table fields − this is a special type of variable which holds anything except nil including functions.

Variable Definition in Lua

A variable definition in Lua informs the interpreter where and how much to create the storage for a variable. A variable definition has an optional type and it consists of a list of one or more variables of that type as shown below

Here, type is optionally local or type specified making it global and variable_list may contain one or more identifier names separated by commas. Some valid declarations are shown here

Line local i, j both are used for declaring and defining the variables i and j; which inform the interpreter to create variables named i, j and limits the scope to be local.

Variables can be initialized (assigned an initial value) in their declaration. Initializer will consist of an equal sign followed by a constant expression as shown below

Some examples are

For definition without an initializer − variables with static storage duration are implicitly initialized with nil.

Variable Declaration in Lua

In the above examples, assignments for multiples variables will follow a variable_list and value_list format. In the above example local d, f = 5,10 we have d and f in variable_list and 5 and 10 in values list.

Value assigning in Lua will take place like first variable in the variable_list with first value in the value_list and so on. Hence, value of d is 5 and value of f is 10.


Try below example, where variables are declared at the top, but they have been defined and initialized inside main function

When the above code is built and executed, it produces below result

Lvalues and Rvalues in Lua

Lua has two kinds of expressions

  • lvalue − Expressions which refers to a memory location is called "lvalue" expression. An lvalue can appear to either the left-hand or right-hand side of an assignment.
  • rvalue − Term rvalue is used for referring to a data value which is stored at some address in memory. rvalue is an expression which will have a value assigned to it, this means that an rvalue may appear on the right-hand side, but not on the left-hand side of an assignment.

Variables are lvalues and they can appear on the left-hand side of an assignment. Numeric literals are rvalues and they will not be assigned and cannot appear on the left-hand side. Below is a valid statement

But below shown is not a valid statement and it might generate a build-time error

In Lua programming language, apart from the above types of assignment, it is possible to have multiple lvalues and rvalues in the same single statement. It is shown below.

In the above statement, 20 is assigned to g and 30 is assigned to l.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Lua (programming language) Topics