When using either the JScript or JavaScript engine, you must specify the name of each variable that the script will use. For JScript in particular, you may also specify the data type of the variable, but JavaScript does not implement strict type declaration. You can accomplish the naming of variables with the var statement:
JScript example:
var counter : int;
JavaScript example:
var counter;
In the JScript example, this will declare a variable counter to be of type integer (go to Null for more information). Here it is not given an initial value, and will assume the default value for integers which is 0.
In the JavaScript example, this will declare a new variable. Since JavaScript does not have explicit type declaration, it will not be given a default value, and instead will evaluate to undefined.
You can also assign an initial value these variables like this:
JScript example:
var counter : int = 1;
JavaScript example:
var counter = 1;
Constants are declared in the same way, with the keyword const, and must be initialized. A constant's value cannot be changed, whereas the value of a variable can.
JScript example:
const maxSelected : int = 100;
JavaScript example:
const maxSelected = 100;
For JScript, when you declare a variable or constant of a specific type, the value you as2sign to it must be valid for that type. You cannot declare an integer variable and try to assign a string value like "This is a string" to it.
For JavaScript, there is a bit more flexibility in the assignment when it comes to types. However, trying to assign a string to a variable, and then using it like an integer will likely cause errors or unexpected behavior, and should be avoided.
You can make several declarations in the same row by listing them separated by commas:
JScript example:
var counter : int = 1,sumOfAllAnswer : int = 0;
JavaScript example:
var counter = 1, sumOfAllAnswer = 0;
This will give code that is harder to read so it is recommended to separate them on several lines instead:
JScript example:
var counter : int = 1;
var sumOfAllAnswer : int = 0;
JavaScript example:
var counter = 1;
var sumOfAllAnswer = 0;
Additional Notes on JScript Type Declaration
When using JScript, another reason for declaring variables across multiple rows is because it prevents you from creating errors that are hard to spot. Type annotation applies only to the variable that immediately precedes it. In the following code, x is an Object because that is the default type and x does not specify a type, while y is an int.
JScript example:
var x, y : int;
For JScript, you do not need to use typed variables, but scripts that use untyped variables are slightly slower and more prone to errors. This is not a point of worry for JavaScript, as it does not have the concept of typed variables.
JScript example:
var counter;
In JScript, without a specified data type, the default type for a variable or constant is Object. Without an assigned value, the default value of the variable is undefined.
You can give a variable an initial value without declaring its type, similar to JavaScript:
var counter = 0;
Untyped constants are defined in the same way:
const maxSelected = 100;