Share via


Type Conversion

Type conversion is the process of changing a value from one type to another. For example, you can convert the string "1234" to a number. Furthermore, you can convert data of any type to the String type. Some type conversions will never succeed. For example, a Date object cannot be converted to an ActiveXObject object.

Type conversions may either be widening or narrowing: widening conversions never overflow and always succeed, whereas narrowing conversions entail the possible loss of information and may fail.

Both types of conversion may be explicit (using the data type identifier) or implicit (without the data type identifier). Valid explicit conversions always succeed, even if it results in a loss of information. Implicit conversions succeed only when the process loses no data; otherwise they fail and generate a compile or run-time error.

Lossy conversions happen when the original data type does not have an obvious analog in the target conversion type. For example, the string "Fred" cannot be converted to a number. In these cases, a default value is returned from the type conversion function. For the Number type, the default is NaN; for the int type, the default is the number zero.

Some types of conversions, such as from a string to a number, are time-consuming. The fewer conversions your program uses, the more efficient it will be.

Implicit Conversions

Most type conversions, such as assigning a value to a variable, occur automatically. The data type of the variable determines the target data type of the expression conversion.

This example demonstrates how data can be implicitly converted between an int value, a String value, and a double value.

var i : int;
var d : double;
var s : String;
i = 5;
s = i;  // Widening: the int value 5 coverted to the String "5".
d = i;  // Widening: the int value 5 coverted to the double 5.
s = d;  // Widening: the double value 5 coverted to the String "5".
i = d;  // Narrowing: the double value 5 coverted to the int 5.
i = s;  // Narrowing: the String value "5" coverted to the int 5.
d = s;  // Narrowing: the String value "5" coverted to the double 5.

When this code is compiled, compile-time warnings may state that the narrowing conversions may fail or are slow.

Implicit narrowing conversions may not work if the conversion requires a loss of information. For example, the following lines will not work.

var i : int;
var f : float;
var s : String;
f = 3.14;
i = f;  // Run-time error. The number 3.14 cannot be represented with an int.
s = "apple";
i = s;  // Run-time error. The string "apple" cannot be converted to an int.

Explicit Conversions

To explicitly convert an expression to a particular data type, use the data type identifier followed by the expression to convert in parentheses. Explicit conversions require more typing than implicit conversions, but you can be more certain of the result. Furthermore, explicit conversions can handle lossy conversions.

This example demonstrates how data can be explicitly converted between an int value, a String value, and a double value.

var i : int;
var d : double;
var s : String;
i = 5;
s = String(i);  // Widening: the int value 5 coverted to the String "5".
d = double(i);  // Widening: the int value 5 coverted to the double 5.
s = String(d);  // Widening: the double value 5 coverted to the String "5".
i = int(d);     // Narrowing: the double value 5 coverted to the int 5.
i = int(s);     // Narrowing: the String value "5" coverted to the int 5.
d = double(s);  // Narrowing: the String value "5" coverted to the double 5.

Explicit narrowing conversions will usually work, even if the conversion requires a loss of information. Explicit conversion cannot be used to convert between incompatible data types. For example, you cannot convert Date data to or from RegExp data. In addition, conversions are not possible for some values because there is no sensible value to which to convert. For example, an error is thrown when attempting to explicitly convert the double value NaN to a decimal. This occurs because there is no natural decimal value that could be identified with NaN.

In this example, a number with a decimal part is converted to an integer, and a string is converted to an integer.

var i : int;
var d : double;
var s : String;
d = 3.14;
i = int(d);
print(i);
s = "apple";
i = int(s);
print(i);

The output is

3
0

The behavior of the explicit conversion depends on both the original data type and the target data type.

See Also

Concepts

Type Annotation

Reference

undefined Property

Other Resources

JScript Data Types