# Language Reference - Variables

A variable is just a place to store data in memory so that it can be accessed quickly. Think of it as a mailbox in memory that you can put information in or take information out of. For example you might create a variable to store the number a user's response to a question, or the result to a math equation.

Each variable has a name (again, similar to a mailbox) and must start with the $ character and may only contain letters, numbers and the underscore _ character. Here are some example names:

$var1

$my_variable

Each variable is stored as a variant.

## Declaring Variables

Variables are declared and created with the Dim, Local and Global keywords:

Dim $var1

Or you can declare multiple variables at once:

Dim $var1, $myvariable

You can also assign a variable without declaring it first, but many prefer explicit declarations.

$var1 = "create and assign"

## Scope

A variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the Global scope and can be read or changed from anywhere in the script.

If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.

By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name (in which case the global variable is reused). This can be altered by using the Local and Global keywords to declare variables and force the scope you want.

## Arrays

An Array is a variable containing series of data elements of the same type and size. Each element in this variable can be accessed by an index number.
An example:
Let's say you want to store these series of characters: "A", "U", "T", "O", "I", "T" and "3".
You could use seven separate variables to do so, but using an Array is more efficient:

$Array[1]="A"
$Array[2]="U"
..etc..
$Array[7]="3"

To access a specific value in an Array, you only have to know the index number:

$MyChar=$Array[3]

This results in $MyChar containing the letter "T" (See also: 'operators').
The index number can also be substituted by another variable or an expression, so you can build complex ways to assign or access elements in an array.
Arrays can also be multi dimensional, when you use multiple series of index numbers, like:

$Array[1][1]="Upper-Left"
$Array[2][1]="Lower-Left"
$Array[1][2]="Upper-Right"
$Array[2][2]="Lower-Right"

(These values are just examples)
You can use up to 64 dimensions in an Array.
Before you can start using Arrays in your script, you must define their bounds using the 'Dim' keyword.

## Data types in Arrays

It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays:

$Array[1]=1
$Array[2]=true
$Array[3]="Text"
$Array[4]=$AnotherArray

This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array. Especially the use of an Array inside another Array will severely affect the execution speed of your script.