Declaring a variable
You can store values in ‘variables’. Variables are used in your Scripts to make things happen, e.g. to perform calculations (numbervariables), create words (string variables), and set other form field values.
A variable does not exit in a script until it is ‘declared’, i.e. ‘brought to life’. A variable is declared as follows:
var name; //This declares the variable ‘name’
var number; //This declares the variable ‘number’
var name, number; //This declares multiple variables at once, i.e. ‘name’ and ‘number’. You need to include the comma between the variables
Initialisation of a variable
When a variable is first declared it can also be initialized with a starting value:
var name = "Tom"; //This will create the variable ‘name’ and give it a starting value of ‘Tom’
var number = 20; //This will create the variable ‘number’ and give it a starting value of ‘20’
var name = "Tom", number = 20; //Multiple variables at once
Assignment of a variable
You can also assign a value to a variable that has already been declared or initialized. When you assign a value to a variable you use the single ‘=’ (equals) sign.
name = "Andrew"; number = 25;
You need to be sure that you have already created the variable before you do this.
Note that if a variable already has a value and you assign a new value to it, this will usually overwrite the original value and replace it with the new value.