if判断中的值是一个对象、非空字符串、非零数字或true时,判断会评估为真;而当值是一个null、undefined、0、false、NaN或空字符串时评估为假。
列:if(name){ alter("true")}
检测属性是否存在时,有另外的两种方式:in和hasOwnProperty(),前者会检测原型属性和自有(实例)属性,后者只检测自有(实例)属性。
console.log("age" in per1); //true
console.log(per1.hasOwnProperty("age")); //true
console.log("toString" in per1); //true
console.log(per1.hasOwnProperty("toString")); //false
二:null值表示一个对象的空指针,typeof 检测null 时返回“object”
var a = null;
alert(typeof a);//"object"
undefined 派生值null ,所以alert(null == undefined);//true
三:Boolean 类型
<pre name="code" class="javascript">var message = "hello word";
var messageBoolean = Boolean(message);//true
四:Number 类型:
1.NaN 即(not a number).NaN 与任何值都不相等,包括本身,alert(NaN == NaN);//false
2.isNaN() ;会帮我们确定这个参数是否“不是数值”。isNaN()在接收到一个值之后,会尝试将这个值转化为数值。某些不是数值的值会直接转换为数值,列如:字符串“10”
或Boolean值。而任何不能被转换为数值的值都会导致这个函数返回true。
alert(isNaN(NaN)) //true
alert(isNaN(10)) //false
alert(isNaN("10")) //false
alert(isNaN("helloword")) //true
alert(isNaN(true)) //false