Share via


Declaring JScript Variables and Constants

A JScript program must specify the name of each variable that the program will use. In addition, the program may specify what data type each variable will store. Both of these tasks are accomplished with the var statement.

Declaring Typed Variables and Constants

In JScript, you can declare a variable and concurrently declare its type using type annotation. In the following example, the variable count is declared to be of type int (integer). Since no initial value is provided, count has the default value of int, which is 0 (zero).

var count : int; // An integer variable.

You can also assign an initial value to a variable:

var count : int = 1; // An initialized integer variable.

Constants, which are declared in much the same way as variables, must be initialized. Once a constant value is defined, its value cannot be changed. For example:

const daysInWeek : int = 7;            // An integer constant.
const favoriteDay : String = "Friday"; // A string constant.
const maxDaysInMonth : int = 31, maxMonthsInYear : int = 12

Of course, when you declare a variable of a specific type, the assigned value must make sense for that type. For example, it does not make sense to assign a character string value to an integer variable. When you do this, the program throws a TypeError exception that indicates a type mismatch in your code. A TypeError is one kind of exception or error that can occur in a running script. A catch block can catch exceptions thrown by a JScript program. For more information, see try...catch...finally Statement.

You can concurrently declare the type and initial value of multiple variables, although it is easier to read code when each declaration is on a separate line. For example, this code segment is hard to read:

var count : int = 1; amount : int = 12, level : double = 5346.9009 

The following code segment is easier to read:

var count : int = 1;
var amount : int = 12;
var level : double = 5346.9009;

Something else to keep in mind when declaring several variables on a single line is that a 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.

var x, y : int;

Declaring Untyped Variables and Constants

You do not need to use typed variables, but programs that use untyped variables are slower and more prone to errors.

The following simple example declares a single variable named count.

var count;  // Declare a single declaration.

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. The following code demonstrates these defaults for a command-line program:

var count; // Declare a single declaration using default type and value.
print(count); //Print the value of count.
print(typeof(count)); // Prints undefined.

You can give a variable an initial value without declaring its type:

var count = 1; // An initialized variable.

The following example declares several variables using a single var statement:

var count, amount, level;  // multiple declarations with a single var keyword.

To declare a variable and initialize it without assigning it a particular value, assign it the JScript value null. Here is an example.

var bestAge = null;

A declared variable without an assigned a value exists but has the JScript value undefined. Here is an example.

var currentCount;
var finalCount = 1 * currentCount; // finalCount has the value NaN since currentCount is undefined.

In JScript, the main difference between null and undefined is that null converts to zero (although it is not zero), while undefined converts to the special value NaN (Not a Number). Ironically, a null value and an undefined value always compare as equal when using the equality operator (==).

The process of declaring untyped constants is similar to the process of declaring variables, but you must provide an initial value for untyped constants. For example:

const daysInWeek  = 7;
const favoriteDay  = "Friday";
const maxDaysInMonth  = 31, maxMonthsInYear = 12

Declaring Variables Without var

You can declare a variable without using the var keyword in the declaration and assign a value to it. This is known as an implicit declaration, and it is not recommended. An implicit declaration creates a property of the global object with the assigned name; the property behaves like a variable with global scope visibility. When you declare a variable at the procedure level, though, you typically do not want it to be visible at the global scope. In this case, you must use the var keyword in your variable declaration.

noStringAtAll = ""; // The variable noStringAtAll is declared implicitly.

You cannot use a variable that has never been declared.

var volume = length * width; // Error - length and width do not yet exist.

Note

Declaring variables without the var keyword generates a compile-time error when running in fast mode, the default mode for JScript. To compile a program from the command line that does not use the var keyword, you must turn off the fast option by using /fast-. It is not safe to turn off the fast option in ASP.NET because of threading issues.

See Also

Tasks

How to: Compile JScript Code from the Command Line

Concepts

JScript Identifiers

Other Resources

JScript Variables and Constants

JScript Data Types