您的位置:首页 > Web前端 > JavaScript

javascript 中 typeof 的使用

2016-06-03 08:22 555 查看
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是一个操作符而不是函数,因此圆括号可以省略,而不是必须的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: