Codecademy Logo

Variables and Data

Type Coercion

Lua performs automatic type conversions between numbers and strings. Strings will be converted into a number data type if used with arithmetic operators. Whereas numbers will be converted into a string data type if used with concatenation.

-- Conversion of a number type to string type
print("The number value " .. 5 .. " will be converted into a string")
-- Output: The number value 5 will be converted into a string
-- Conversion of a string type to number type
print("1" + 1)
-- Output: 2

tostring() and tonumber()

The built-in functions tostring() and tonumber() can explicitly convert data types. tostring() can convert any data type into a string type. Likewise, tonumber() can convert any data type into a number type.

In the example, the value 3 in variable b is converted into a string type. While the value "2" in variable a is converted into a number type.

a = "2"
b = 3
print("There are " .. tostring(b) .. " bees")
-- Output: There are 3 bees
print(tonumber(a) + 1)
-- Output: 3

Variables

Variables are used to maintain code-reusability by storing, retrieving, and manipulating data. Any data stored inside of a variable can be used throughout the program.

-- Declaring a variable with a number value
numOfSlices = 8
-- Accessing the variable and reusing it
print("This pizza has " .. numOfSlices .. " slices!")
print(numOfSlices .. " is the best number!")
-- Output:
-- This pizza has 8 slices!
-- 8 is the best number!

Variable Declaration

Variables are declared with a name and are assigned a value using an equal sign (=).

userName = "flap_jacks"

Data Types

Variables can contain values of any data type such as string, number, boolean, and nil.

-- A variable storing a number type
someNumber = 8
-- A variable storing a string type
someString = "Anteater"
-- A variable storing a boolean type
someBoolean = true
-- A variable storing a nil type
someNil = nil

type()

The type() function returns a string with the data type of the argument passed to it.

a = 10
print(type(a)) -- Output: number
a = "croissant"
print(type(a)) -- Output: string
a = true
print(type(a)) -- Output: boolean
a = nil
print(type(a)) -- Output: nil

Concatenation

Multiple values can be combined together to produce a single string using the concatenation operator (..). The concatenation operator can work with both string and number data types.

num = 10
location = "Blockbuster"
print("There are " .. num .. " kids in " .. location )
-- Output: There are 10 kids in Blockbuster

Arithmetic Operators

Arithmetic operators are used to modify numerical values. The arithmetic operators supported in Lua are:

  • addition (+)
  • subtraction (-)
  • multiplication (*)
  • division (/)
  • exponentiation (^)
  • remainder (%)
  • negation (-)
firstNumber = 10
secondNumber = 5
-- Addition
print(firstNumber + secondNumber) -- Output: 15
-- Subtraction
print(firstNumber - secondNumber) -- Output: 5
-- Multiplication
print(firstNumber * secondNumber) -- Output: 50
-- Division
print(firstNumber / secondNumber) -- Output: 2
-- Exponentiation
print(firstNumber ^ secondNumber) -- Output: 100000
-- Remainder
print(firstNumber % secondNumber) -- Output: 0
-- Negation
print(-firstNumber) -- Output: -10

Variable Reassignment

Variables can store values of any data type (number, string, boolean, and nil). After a value is assigned, the variable can be updated to a new value as needed.

-- Initial assignment
userName = "flap_jacks"
-- Reassignment
userName = "pancakes"

Data

Data is information stored by a computer.

-- The following are all example of data
99
1.3
"a FuNKY sTriNg"
"A longer, more formal, less funky string"
false
nil

Data Types

Data always has a data type that determine what operations can be performed on the data. Basic data types are number, string, boolean, and nil.

-- An assortment of data with different data types:
11 -- Number
"Hello!" -- String
false -- Boolean
nil -- Nil

type()

The type() function returns a string with the data type of the argument passed to it.

print(type("Candied Apples")) -- Output: string
print(type(45)) -- Output: number

Boolean Data Type

The boolean data type can be either true or false.

true -- Note that boolean values do not have single or double quotes wrapped around it
false

Number Data Type

The number data type is any numeric value including integers and decimals.

101
13.56

String Data Type

The string data type is any sequence of zero or more characters surrounded by single quotes ' or double quotes ".

'Hello, world!'
"Goodnight, universe!"

Nil Data Type

The nil data type represents the absence of a value.

nil

Arithmetic Operators

Arithmetic operators are used to transform number data, the arithmetic operators supported are:

  • addition (+)
  • subtraction (-)
  • multiplication (*)
  • division (/)
  • exponentiation (^)
  • remainder (%)
  • negation (-)
-- Addition
10 + 2
-- Subtraction
90-23
-- Multiplication
3 * 4
-- Division
64 / 8
-- Exponentiation
4 ^ 2
-- Remainder
16 % 2
-- Negation
-5

Expression

An expression is any code that resolves to a value.

1 + 1 -- Expression resolves to the value 2.

Learn more on Codecademy