By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

AutoIt Variables

How to declare and work with variables in AutoIt, as well as some background information.

Edited: 2015-04-01 11:57

AutoIt Logo

Its important to know about Variables in AutoIt, as they can be used to store information for later use.

Variables are declared by typing in the name that you want, followed by an equals sign and its value. You can also declare a variable as empty, usually you would do that when you want to fill it out later in the script.

Variables begin with the dollar sign, $, and can only contain letters, numbers, and underscore (_).

How to Declare Variables

The below is a variable getting declared with the default text "This is a Test". It is possible to assign a variable without declaring it, there would be times however, where you would want to declare it first. When working with user made functions for example. But that will be covered in another Tutorial.

$My_Variable_Name = 'This is a Test'

Generally thats how you would assign a variable, and thats all beginners need to know for now. No need to complicate things right?

Joining variables

Aka. String Concatenation

When you want to join multiple variables together, use the and sign (&), example shown below.

$word1 = 'Your '
$word2 = 'friend'
$phrase = $word1 & $word2

To add even more variables, just add another ampersand, followed by a string in quotes or another variable.

$word1 = 'Your '
$word2 = 'friend'
$phrase = $word1 & $word2 & 'String in Quotes'

Strings and Numbers

Some languages have different datatypes for variables, depending on whether they will be used to contain text or numbers. A string usually just consists of text and other characters. But a string can also be an image, video or mp3 file, Etc. Of cause those would be "very large" strings, but it should make it clear that strings have many uses, besides containing smaller text messages.

AutoIt only has a single datatype called a Variant. If you try to multiply two variants, they will be treated as numbers. And if you join two variables, they will be treated as strings.