JScript  

Type Annotation

[This is preliminary documentation and subject to change]

Binds variables to specific types.

var name [: type[[ ]]][ = value]

function name(parmlist) [: type] {

[body]

}

Arguments

name

Required. Name of the variable or function.

type

Optional. Specifies the type associated with the variable or the return type of a function. Type names may be Boolean, Number, String, int, long, float, double, Object, Date, Array, or Function. Additional types can be introduced into JScript either by importing a namespace that contains the new type, or by declaring user-defined types as classes.

[ ]

Optional. Denotes declaration of a one-dimensional array of the specified type.

value

Optional. Value assigned to a variable.

parmlist

Optional. Formal parameter list of the function.

body

One or more statements that are the code of the function being declared.

Remarks

Variables that have type annotations cannot be deleted, and their type cannot be changed once they have been declared. If a variable in a given scope is declared using a type annotation, it must be declared only once. Untyped variables can be declared multiple times, but then all declarations must be untyped.

If no type is specified, the default is Object. Uninitialized variables are given the value undefined, which is then coerced into a suitable value. Number variables are coerced to NaN and String variables are coerced to "".

Example

The following example illustrates the use of type annotations with variables and functions:

// Declare an integer variable.
var myInt : int;
//Declare an integer variable and initialize it.
var anotherInt : int = 42;
// Declare a function that takes an int and returns a String.
function Ordinal(num : int) : String{
switch(num % 10)
  {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
  }
}

Requirements

Version 7

See Also

var Statement | function Statement