a faster, safer, easier JavaScript
Powered by Oktavia

Type Conversion

The as operator is used for: conversion between primitive types (including Nullable and variant), down-casting of object types.

The conversion rules between primitive types are defined as follows. If the source type is a Nullable type and if the value is null, a run-time exception is raised under debug builds. The behavior is unspecified for release builds.

The result of conversion from a variant type depends on the result of the typeof operator applied to the variant.

Down-casting of an object type returns a reference to the casted object if successful, otherwise null.

Table 1. Conversion between the Primitive Types using the As Operator
Source TypeDestination TypeResult
booleannumber0 if false, 1 if true
booleanintsame as above
booleanstring"false" if false, "true" if true
numberbooleanfalse if 0 or NaN, otherwise true
numberintfractional part is removed, becomes 0 if NaN, may get rounded to between -231 and 231-1
numberstringconverted to string representation
intbooleanfalse if 0, otherwise true
intnumberconverted to number of same value
intstringconverted to string representation
stringbooleanfalse if the string is empty, otherwise true
stringnumber0 if the string is empty, a number if the string can be parsed as a string, otherwise NaN
stringintequivalent to: as number as int
Table 2. Conversion from Variant using the As Operator
Result of typeof(variant)Destination TypeResult
"boolean"any primitive typeequivalent to the result of: boolean as type
"number"any primitive typeequivalent to the result of: number as type
"string"any primitive typeequivalent to the result of: string as type
"object"any primitive typedepends on the actual type of the value
"object"any object typereference to the object if the value is an object of the specified type, otherwise null