js中typeof的用法
2016-09-04 15:07
399 查看
typeof运算符介 绍:
typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。
它返回值是一个字符串,该字符串说明运算数的类型。
你 知道下面typeof运算的结果吗?
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);
看 看你会几个?
如果看了以后,不是很明白的话,请看下面(明白的人就不用往下看了):
typeof是一个一元运算符,它返回的结果 始终是一个字符串,对不同的操作数,它返回不同的结果。
具体的规则如下:
一、对于数字类型的操作数而言, typeof 返回的值是 number。比如说:typeof(1),返回的值就是number。
上面是举的常规数字,对于非常规的数字类型而言,其结果返回的也是number。比如typeof(NaN),NaN在
JavaScript中代表的是特殊非数字值,虽然它本身是一个数字类型。
在JavaScript中,特殊的数字类型还有几种:
Infinity 表示无穷大特殊值
NaN 特殊的非数字值
Number.MAX_VALUE 可表示的最大数字
Number.MIN_VALUE 可表示的最小数字(与零最接近)
Number.NaN 特殊的非数字值
Number.POSITIVE_INFINITY 表示正无穷大的特殊值
Number.NEGATIVE_INFINITY 表 示负无穷大的特殊值
以上特殊类型,在用typeof进行运算进,其结果都将是number。
二、对于字符串类型, typeof 返回的值是 string。比如typeof("123")返回的值是string。
三、对于布尔类型, typeof 返回的值是 boolean 。比如typeof(true)返回的值是boolean。
四、对于对象、数组、null 返回的值是 object 。比如typeof(window),typeof(document),typeof(null)返回的值都是object。
五、 对于函数类型,返回的值是 function。比如:typeof(eval),typeof(Date)返回的值都是function。
六、如 果运算数是没有定义的(比如说不存在的变量、函数或者undefined),将返回undefined。比如:typeof(sss)、typeof(undefined)都返回undefined。
看完了六条规则,再回头看一下,是不是很简单了……
下面 我们将用程序代码验证一下:
<script>
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>
typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。
它返回值是一个字符串,该字符串说明运算数的类型。
你 知道下面typeof运算的结果吗?
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);
看 看你会几个?
如果看了以后,不是很明白的话,请看下面(明白的人就不用往下看了):
typeof是一个一元运算符,它返回的结果 始终是一个字符串,对不同的操作数,它返回不同的结果。
具体的规则如下:
一、对于数字类型的操作数而言, typeof 返回的值是 number。比如说:typeof(1),返回的值就是number。
上面是举的常规数字,对于非常规的数字类型而言,其结果返回的也是number。比如typeof(NaN),NaN在
JavaScript中代表的是特殊非数字值,虽然它本身是一个数字类型。
在JavaScript中,特殊的数字类型还有几种:
Infinity 表示无穷大特殊值
NaN 特殊的非数字值
Number.MAX_VALUE 可表示的最大数字
Number.MIN_VALUE 可表示的最小数字(与零最接近)
Number.NaN 特殊的非数字值
Number.POSITIVE_INFINITY 表示正无穷大的特殊值
Number.NEGATIVE_INFINITY 表 示负无穷大的特殊值
以上特殊类型,在用typeof进行运算进,其结果都将是number。
二、对于字符串类型, typeof 返回的值是 string。比如typeof("123")返回的值是string。
三、对于布尔类型, typeof 返回的值是 boolean 。比如typeof(true)返回的值是boolean。
四、对于对象、数组、null 返回的值是 object 。比如typeof(window),typeof(document),typeof(null)返回的值都是object。
五、 对于函数类型,返回的值是 function。比如:typeof(eval),typeof(Date)返回的值都是function。
六、如 果运算数是没有定义的(比如说不存在的变量、函数或者undefined),将返回undefined。比如:typeof(sss)、typeof(undefined)都返回undefined。
看完了六条规则,再回头看一下,是不是很简单了……
下面 我们将用程序代码验证一下:
<script>
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>
相关文章推荐
- JSONObjetc和String Map Bean互转,JSONArray和String List互转
- JavaScript charAt()、indexOf()、substr()、substring()
- JS实现屏蔽网页右键复制及ctrl+c复制的方法【2种方法】
- JavaScript的继承和多态
- cojs1101. [Vijos1369] 难解的问题==codevs 2188 最长上升子序列
- The switch statement of JavaScript
- JavaScript算法系列之快速排序(Quicksort)算法实例详解
- Ext.js入门:面板(五)
- JavaScript 快速排序(Quicksort)
- js 正则表达式 判断整数与小数
- javascript语句条件语句,循环语句,跳转语句
- JS map 的一些概念
- Electron: HTML + Javascript 开发桌面应用程序
- js的自定义事件
- JS中的bind方法学习
- 超实用的JavaScript代码段 Item8 -- js对象的(深)拷贝
- jsp杂记
- js随机生成颜色
- js闭包理解
- JavaScript中变量提升 Hoisting