javascript 中 typeof 的使用

时间:2023-03-09 16:50:15
javascript 中 typeof 的使用

javascript 中 typeof 的使用

javascript有五种基本的数据类型(简单数据类型),它们分别是:String、Undefined、Null、Boolean和Number。还有一种复杂数据类型Object。

typeof可以检测给定变量的数据类型。

对一个值使用typeof操作符可能返回下列某个字符串:string、number、boolean、undefined、function和object。

<script type="text/javascript">
function testTypeOf() {
var message = "hello";
alert(typeof(message)); //string
var num = 110;
alert(typeof(num)); //number
var boo = false;
alert(typeof(boo)); //boolean
var unde;
alert(typeof(unde)); //undefined
var fun = function(){return;};
alert(typeof(fun)); //function
var nul = null;
alert(typeof(nul)); //object alert(typeof 9);
}
testTypeOf();
</script>

注意:typeof是一个操作符而不是函数,因此圆括号可以省略,而不是必须的。