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

typeof运算符---JavaScript

2016-09-04 20:21 465 查看
前言:

每一种计算机语言除了有自己的数据结构外,还具有自己所支持的数据类型。在JavaScript脚本语言中,采用弱类型方式,即一个变量不必首先做声明,可以在使用或赋值时再确定其数据类型,当然也可以先声明该变量的类型。要想知道一个变量或值是什么类型的需要调用typeof运算符。

typeof运算符:

typeof运算符有一个参数,即要检查变量的值。对变量或值调用typeof运算符将返回如下值之一:

1)undefined:变量为Undefined类型;

2)boolean:变量是Boolean类型的;

3)number:变量是Number类型的;

4)string:变量是String类型的;

5)object:变量是一种引用类型或Null类型的。

具体使用:

<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
typeof(1);
typeof(NaN);
typeof(Number.MIN_VALUE);
typeof(Infinity);
typeof("123");
typeof(true);
typeof(window);
typeof(document);
typeof(null);
typeof(eval);
typeof(Date);
typeof(sss);
typeof(undefined);
document.write("typeof(1):"+typeof(1)+"<br/>");
document.write("typeof(NaN):"+typeof(NaN)+"<br/>");
document.write("typeof(Number.MIN_VALUE):"+typeof(Number.MIN_VALUE)+"<br/>");
document.write("typeof(Infinity):"+typeof(Infinity)+"<br/>");
document.write("typeof(\"123\"):"+typeof("123")+"<br/>");
document.write("typeof(true):"+typeof(true)+"<br/>");
document.write("typeof(window):"+typeof(window)+"<br/>");
document.write("typeof(document):"+typeof(document)+"<br/>");
document.write("typeof(null):"+typeof(null)+"<br/>");
document.write("typeof(eval):"+typeof(eval)+"<br/>");
document.write("typeof(Date):"+typeof(Date)+"<br/>");
document.write("typeof(sss):"+typeof(sss)+"<br/>");
document.write("typeof(undefined):"+typeof(undefined)+"<br/>");
</script>
</body>
</html>


运行结果:



 

 

 

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