Microsoft® JScript
JScript Variables
Language Reference 


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. 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 variable mim is cast to string type.
// The sentence in quotes, the value of which is assigned to mim, is a string literal.

var ror = 3;        // The variable ror is cast to numeric type.
var nen = true;        // The variable nen is cast to Boolean type.

var fif = 2.718281828        // The variable fif is cast to 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 must follow certain rules:

Some examples of valid variable names:

Some invalid variable names:

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.


var zaz = null;
var notalot = 3 * zaz;        // At this point, notalot becomes 0.
If you declare a variable without assigning any value to it, it exists but is undefined.


var godot;
var waitingFor = 1 * godot;  // Generates an error because godot is undefined.
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.


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 type. Instead, they can be thought of as having a type equivalent to the data they hold. 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.


var theFrom = 1;
var theTo = 10;
var doWhat = "Count from ";
doWhat += theFrom + " to " + theTo + ".";        
After this code is executed, the doWhat variable contains "Count from 1 to 10." The number data have been coerced into string form.


var nowWhat = 0;
nowWhat += 1 + "10";  // In this case, because "10" is a string,
// the "+=" operator concatenates.
After this code is executed, the nowWhat variable contains "0110"&emdash;string data cannot be coerced into numeric form.


var nowThen = 0;
nowThen += 1 + parseInt("10");        // In this case, "+=" performs addition.
After this code is executed, the nowThen variable contains the integer 11.


© 1996 by Microsoft Corporation.


Casa de Bender