typeof定义
typeof是一元运算符,用来返回操作参数的类型(不是值)
检查一个变量是否存在,是否有值
typeof在两种情况下会返回"undefined":
- 一个变量没有被声明的时候,
- 一个变量没有赋值的时候
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<script>
function testTypeOf() {
//返回boolean
console.log(typeof(true)); //返回number
console.log(typeof(1.2)); //返回string
console.log(typeof("foo")); //返回object
console.log(typeof(new String("foo")));
console.log(typeof(new Date()));
console.log(typeof(null));
console.log(typeof(new Number(1.2)));
console.log(typeof(new Boolean(true)));
console.log(typeof(new Array(1, 2, 3)));
console.log(typeof(new Error()));
console.log(typeof([1,2,3])); console.log(typeof(/abc/g));
console.log(typeof(new RegExp("meow")));
console.log(typeof({}));
console.log(typeof(new Object())); //返回function
console.log(typeof(new Function(""))); //返回undefined
var str;
console.log(typeof(str));
console.log(typeof(strLen));
}
</script>
</head> <body>
<button onclick="testTypeOf()">点击</button>
</body> </html>