Microsoft® JScript®
JScript Variables
|
JScript Tutorial; Previous Next |
Variables are used in Microsoft JScript to store values in your scripts. They are a way to retrieve and manipulate values using names. When used effectively then can help in understanding what a script does.
Declaring Variables
Although not required, it is considered good practice to declare variables before using them. You do this using the var statement. The only time you must use the var statement is when declaring variables that are local to a function. Local variables are those that are only within the function. At all other times, using the var statement to declare variables before their use is a recommended practice.The following code examples are of variable declaration:
var mim = "A man, a plan, a canal, Panama!"; // The value stored in mim is of string type. // The sentence in quotes, the value of which is assigned to mim, is a string literal. var ror = 3; // The value stored in ror has numeric type. var nen = true; // The value stored in nen has Boolean type. var fif = 2.718281828 // The value stored in fif has numeric type.
Naming Variables
JScript is a case-sensitive language, so naming a variable myCounter is different from naming it MYCounter. In addition, variable names, which can be of any length, must follow certain rules:Some examples of valid variable names:
- The first character must be a letter (either uppercase or lowercase) or an underscore (_), or a dollar sign ($).
- Subsequent characters can be letters, numbers, underscores, or dollar signs.
- The variable name can't be a reserved word.
Some invalid variable names:
- _pagecount
- Part9
- Number_Items
In instances in which you want to declare a variable and initialize it, but without giving it any particular value, you may assign it a special value, null.
- 99Balloons // Starts with a number.
- Smith&Wesson; // Ampersand (&) is not a valid character for variable names.
If you declare a variable without assigning any value to it, it exists but is undefined.var zaz = null; var notalot = 3 * zaz; // At this point, notalot becomes 0.
You can declare a variable implicitly (without using var) by assigning a value to it. You cannot, however, use a variable that has never been declared at all. To do so generates an error at runtime.var godot; var waitingFor = 1 * godot; // Places the value NaN in waitingFor as godot is undefined.
lel = ""; // The variable lel is declared implicitly. var aMess = vyv + zez; // Generates an error because vyv and zez don't exist.
Coercion
As JScript is a loosely-typed language, variables in JScript technically have no fixed type. Instead, they have a type equivalent to the type of the value they contain. It is possible, under some circumstances, to force the automatic conversion (or coercion) of a variable or a piece of data into a different type. Numbers can easily be included in strings, but strings cannot be included directly in numbers, so explicit conversion functions, parseInt() and parseFloat(), are provided.After this code is executed, the doWhat variable contains "Count from 1 to 10." The number data have been coerced into string form.var theFrom = 1; var theTo = 10; var doWhat = "Count from "; doWhat += theFrom + " to " + theTo + ".";
After this code is executed, the nowWhat variable contains "0110". The following steps are followed to arrive at this result:var nowWhat = 0; nowWhat += 1 + "10"; // In this case, because "10" is a string, // the "+=" operator concatenates.
- Look at the types of 1 and "10". The "10" is a string, the 1 is a number, so the number is coerced into a string.
- As the values on either side of the + operator are both strings, do a string concatenation. This results in "110"
- Look at the types of the values on either side of the +=. nowWhat contains a number, and "110" is a string, so convert the number to a string.
- As there are now strings on either side of the += operator, do a string concatentation. This results in "0110".
- Store this result in nowWhat.
After this code is executed, the nowThen variable contains the integer 11.var nowThen = 0; nowThen += 1 + parseInt("10"); // In this case, "+=" performs addition.