Share via


typeof 運算子

傳回可識別運算式之資料型別的字串。

typeof[(]expression[)] ;

引數

  • expression
    必要項。 任何運算式。

備註

typeof 運算子會以字串傳回型別資訊。 typeof 可能會傳回下列八種值:"number"、"string"、"boolean"、"object"、"function"、"date"、"undefined" 和 "unknown"。

在 typeof 語法中的括號是選擇性的。

注意事項注意事項

︰JScript 中的所有運算式都擁有 GetType 方法。 這些方法會傳回運算式的資料型別,而不是代表資料型別的字串。 GetType 方法比 typeof 運算子提供更多的資訊。

範例

下列範例說明如何使用 typeof 運算子。

var x : double = Math.PI;
var y : String = "Hello";
var z : int[] = new int[10];

print("The type of x (a double) is " + typeof(x)  );
print("The type of y (a String) is " + typeof(y) );
print("The type of z (an int[]) is " + typeof(z) );

本程式碼的輸出為:

The type of x (a double) is number

The type of y (a String) is string

The type of z (an int[]) is object

需求

1 版

請參閱

參考

GetType

概念

運算子優先順序

運算子摘要