JS入门——变量的类型、特殊符号、类型转化规则
一、变量类型
1.1总述

1.2代码实现
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><script>// tyoeo可以检测出类型alert(typeof(1));alert(typeof(3.12))alert(typeof("Day day up!"))alert(typeof('Day day up!'))alert(typeof(true))alert(typeof(false))alert(typeof(null))var a;alert(typeof(a))</script></body>
</html>
二、特殊符号:““和”=”
区别:""会进行类型转化后,才进行比较。“=”是直接进行比较。
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>JS特殊运算符</title></head><body><script>var a= 10;// "=="会进行类型转化后,才进行比较。“===”是直接进行比较//只有类型和值相同才会返回true//下面两个式子结果均为true,alert(a == 10);alert(a=="10")// 下面两个式子中,第一个的结果为ftrue,第二个的结果为falsealert(a === 10)alert(a === "10")</script></body>
</html>
三、类型转化
3.1其他类型转化为数字
使用方法:parseint
特点:当前面是数字后面是字母时,后面的字母不会被转化; 当前面是字母时。直接返回NaN:Not a number
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><script>//类型转化,// 其他类型转化为int//使用方法parseint//特点当前面是数字后面是字母时,后面的字母不会被转化// 当前面是字母时。直接返回NaN:Not a numberalert(parseInt("3"))alert(parseInt("3Hello"))alert(parseInt("Hello3"))</script></body>
</html>
3.2其他类型转化为boolean时(含有大量注释)
数字转化为boolean:只有0和NaN为false,其他类型均为true
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><script>//类型转化,// 其他类型转化为int//使用方法parseint//特点当前面是数字后面是字母时,后面的字母不会被转化// 当前面是字母时。直接返回NaN:Not a number// alert(parseInt("3"))// alert(parseInt("3Hello"))// alert(parseInt("Hello3"))// 其他类型转化为boolean类型// 数字转化为boolean:只有0和NaN为false,其他类型均为rue// if (0) {// alert("0没有转化为false")// }// if(NaN){// alert("NaN没有转化为false")// }// if (1) {// alert("1转化为了true,其余都转化为了false")// }// string转化为boolean,只有空字符才为false,其他的都为true// if(""){// alert("空字符没有转化为fase")// }// if ("Hello") {// alert("该字符串转化为了true")// }//null和undefine转化为falseif (null) {alert("null没有转化为fase")}if (undefined) {alert("undefined没有转化为fase")}</script></body>
</html>