# 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 typeprint("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 typeprint("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 valuenumOfSlices = 8
-- Accessing the variable and reusing itprint("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 typesomeNumber = 8-- A variable storing a string typesomeString = "Anteater"-- A variable storing a boolean typesomeBoolean = true-- A variable storing a nil typesomeNil = nil `

### `type()`

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

```a = 10print(type(a)) -- Output: number
a = "croissant" print(type(a)) -- Output: string
a = trueprint(type(a)) -- Output: boolean
a = nilprint(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 = 10location = "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 = 10secondNumber = 5
-- Additionprint(firstNumber + secondNumber) -- Output: 15-- Subtractionprint(firstNumber - secondNumber) -- Output: 5-- Multiplicationprint(firstNumber * secondNumber) -- Output: 50-- Divisionprint(firstNumber / secondNumber) -- Output: 2-- Exponentiationprint(firstNumber ^ secondNumber) -- Output: 100000-- Remainderprint(firstNumber % secondNumber) -- Output: 0-- Negationprint(-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 assignmentuserName = "flap_jacks"

### Data

Data is information stored by a computer.

`-- The following are all example of data991.3"a FuNKY sTriNg""A longer, more formal, less funky string"falsenil`

### 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!" -- Stringfalse -- 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.

`10113.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 (`-`)
`-- Addition10 + 2-- Subtraction90-23-- Multiplication3 * 4-- Division64 / 8-- Exponentiation4 ^ 2-- Remainder16 % 2-- Negation-5`

### Expression

An expression is any code that resolves to a value.

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