typeof检测几种较为特殊的值与Boolean强制转换哪几种值为false Amy

typeof是操作符,不是函数,可以用它来检测给定变量的数据类型,共有6个取值:"undefined"/"number"/"string"/"object"/"function"/"boolean"

下面是几种较为特殊的值

alert(typeof NaN);//number
alert(typeof Number.MAX_VALUE);//number
alert(typeof Number.POSITIVE_INFINITY);//number
var a=function(){
};
alert(typeof a);//function
alert(typeof function(){});//function
alert(typeof {});//object
alert(typeof []);//object
alert(typeof null);//object
//
alert(typeof b);//undefined
var c;
alert(typeof c);//undefined
//未初始化未定义typeof返回的均为undefined,但是它们还是有区别的,如果alert一个从未定义的变量alert(b)//出错
//如果alert一个未初始化的变量alert(c)//undefined

 除了下面5个特殊值,其他任何类型的数据转换为逻辑值时都是true,NaN,null,undefined,0,""

alert(Boolean(NaN));//false
alert(Boolean(undefined));//false
alert(Boolean(""));//false
alert(Boolean(0));//false
alert(Boolean(null));//false
原文地址:https://www.cnblogs.com/amy2011/p/3131636.html