typeof

Summary

Returns the type of an expression at runtime. This operator is deprecated. Use typeid instead.

Syntax

typeof expression

Parameters

expression
Any valid expression.

Description

typeof is deprecated in JS++. typeof uses the JavaScript implementation and types. For compatibility with JS++ types and more accurate runtime typing, use typeid.

typeof returns the type of an expression as a string literal (e.g. "boolean") at the time it is called. For JS++ types, the value returned will always be "internal". For JavaScript types, typeof can return one of the following possible values:

  • "number"
  • "string"
  • "boolean"
  • "function"
  • "object"
  • "undefined"

The type of an expression may be different than the type of the individual variables used in it because typeof returns the type for the final evaluated value of the expression. For example, this code stores bool in z:

1
2
3
4
int x = 2;
int y = 3;
         
string z = typeof(y > x);

If the name of a function is used, the result is "function". However, if a function call is used, the type is based on the function return value. For example:

1
2
3
4
5
6
7
function x() {
    int f = 5;
    return f;
}
 
string y = typeof x;   // "function" will be stored in y
string z = typeof x(); // "number" will be stored in z

Since typeof uses the JavaScript implementation and types, null will return type "object", undefined will return type "undefined", and NaN will return type "number". Instead, use typeid for better JS++ compatibility.

Examples

Variable Type Can Change During Execution
1
2
3
4
5
6
import System;
 
var x;
Console.log(typeof x); // output is "undefined"
x = 3;
Console.log(typeof x); // output is "number"

See Also

Share

HTML | BBCode | Direct Link