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

JavaScript typeof运算符

2017-11-11 10:27 357 查看
常见类型:

number、string、boolean、undefined、object、function

一个变量应该只存放一种类型的数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
var a=12;

alert(typeof a);    //number

a='abc';
//alert(typeof a);  //string

a=true;
//alert(typeof a);  //boolean   布尔

/*window.onload=function ()
{
a=document.getElementById('div1');

alert(typeof a);    //object
};*/

a=function ()
{
alert('abc');
};

//alert(typeof a);  //function

var b;
alert(typeof b);    //undefined
/*
undefined   1.你真的就没定义   2.虽然定义了,但是没给值
*/

</script>
</head>

<body>
<div id="div1">
dfasdf
</div>
</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
/*var arr=[1,2,3,4];

alert(typeof arr);*/

var oDate=new Date();

alert(typeof oDate);
</script>
</head>

<body>
</body>
</html>


参考:JavaScript
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: