判断操作语句
if中的条件先计算表达式,后转换为布尔类型(Boolean),只有 0/NaN/''/null/undefined 是false,其余都是true。
if(0) false
if('3px-3) false '3px'-3 -> NaN -> false
if('3px'+3) true '3px'+3 -> '3px3' -> true
在计算表达式的时候,+ - * / % 都是数学运算,除 + 以外,其余运算符在运算的时候,如果遇到了非数字类型的值,首先会转换为数字类型(Number),然后再进行运算;+ 除了数学相加,还有字符串拼接的作用(如果运算中遇到了字符串,则为字符串拼接)
题:
var num = parseInt('width:35.5px');
if(num==35.5){
alert(0);
}else if(num==35){
alert(1);
}else if(num==NaN){
alert(2);
}else if(typeof num=='number'){
alert(3);
}else{
alert(4);
}
解:alert输出的都是字符串格式的 '3'
检验数据类型
typeof [value] =>检测value的数据类型
使用typeof检测出来的结果是一个字符串,字符串中包含着对应的数据类型,例如:"number"/"string"/"boolen"/"undefined"/"object"/"function"
console.log(typeof []);
//=>"object"
console.log(typeof typeof []);
//=>typeof "object"
//=>"string"