What are the variables in Lua?

Overview

In general, the variable is the value that can change in the program. We can store the value of a variable and reference it in the later section of the program.

In Lua, we don’t indicate the type of variables we create. Though it is not advised to use the same variable to store different data types within the program.

Declare a variable

We cannot use a variable before the declaration. In other words, we should define a variable and then use it within the program. And unlike C or C++, we don’t need to declare all the variables at the beginning of the program.

To declare a variable, we need to write the variable’s name. It is possible to declare a variable with initialization.

first_name and first_name="Michael" are both valid ways to introduce a variable.

Let’s print "Hello World" in the Lua Shell:

print("Hello World")

As expected, it will display "Hello World" on the screen. We write the same "Hello World" program differently by using a variable:

welcome_message = "Hello World"
print(welcome_message)

It also displays "Hello World" on the screen.

The above line of code welcome_message = "Hello World" assigns the "Hello World" string to the welcome_message variable where the assignment operator (=) works from right to left.

Valid variable names

Following are the valid variable names:

  • Variable names can be long
  • Variable names can contain both letters and numbers
  • Variable names are case sensitive, i.e., Arjuna and arjuna and ARJUNA are each treated as three different variables by Lua
  • Variable name cannot start with numbers, i.e., 2018Year is an invalid variable name, whereas Year2018 is a valid one. The user will get a syntax error for an invalid variable name.
  • Keywords (Lua reserve words) can’t be used as variable names like if, class, return, etc., are invalid variable names.

Lua keywords

The following keywords cannot be used as variable names:

and break do
false for function
if in local
nil not or
repeat return then
true until while

If we try to declare the variable as any of the keywords declared above, we’ll get an error stdin:1: unexpected symbol...

Types of variables

Lua supports nil, boolean, number, string, function, thread, and table type of variables.

We can know the type of the variable by a built-in function type (similar to other languages like Python) in Lua.

type(variable_name) returns the type of the value stored in a variable.

Number

A number is a whole number or floating-point number that can be positive, negative, or zero. It can’t be in fractions or decimals. For e.g. -1, 2, 8, 0, -25, -1.0, 2.4, 3.97, -6.00

number_one = 12

String

The string is a continuous sequence of symbols or characters. For example Who Am I, Welcome To The Jungle are examples of string variables.

lua_string = 'Welcome to the jungle'
print(lua_string)
print(type(lua_string))

It’ll display the following on-screen:

Welcome to the jungle
string

The string is the type of variable.

Boolean values

Like other languages, the boolean has two values, true and false. Unlike other languages, Lua considers zero and the empty string true in conditional tests.

a = true
print("The type of the variable is: ")
print(type(a))
print("The value of a is: ")
print(a)

The above code prints the boolean and true on the screen.

Tables

Tables are the only data structure available in Lua. Lua uses associative arrays and has indexed numbers and strings except for nil.

Lua tables have no fixed size and can grow as per our needs. tab = {} is the way to initialize the table variable.

Let’s execute the code below and check:

tab = {}
print("The type of variable is : ", type(tab))

To insert values in table variable, we use tab[1] = 20 and tab['index'] = 'First Index' are valid. Lua uses associative arrays.

tab = {}
tab[1] = "20"
tab['index'] = 'First Index'
print(tab[1])
print(tab['index'])

Functions

In Lua, functions are first-class values and can be stored in variables, passed as arguments to other functions. type and print are the functions in Lua. Let’s assign them to a variable and see the type of the variable:

a = type
b = print
print("The type of variable a and b is:")
print(type(a))
print(type(b))

Free Resources