返回一个用来表示表达式的数据类型的字符串。
typeof[()expression[]] ;
expression 参数是需要查找类型信息的任意表达式。
说明
typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."
typeof 语法中的圆括号是可选项。
var sTemp = "test string";
alert(typeof sTemp);
alert(typeof 95);
看个详细的实例
<html>
<head>
<title>Using typeof to determine the type of variables</title>
<script language="JavaScript1.1">
<!--
var bMyVar = true;var nMyVar = 35;
var sMyVar = "This is a string";
var uMyVar;
-->
</script>
</head>
<body>
<script language="JavaScript1.1">
<!--
document.writeln("bMyVar = " + typeof(bMyVar));document.writeln("<br>nMyVar = " + typeof(nMyVar));
document.writeln("<br>sMyVar = " + typeof(sMyVar));
document.writeln("<br>uMyVar = " + typeof(uMyVar));
-->
</script>
</body>
</html>