📄 1.1.htm
字号:
<html>
<head>
<title>数据类型自动转换</title>
</head>
<body>
<h3>自动数据类型转换示例</h3>
<hr>
<script language="JavaScript">
<!--
//document.write();
document.write("<pre>");
document.write("转换为逻辑型数据:<br><br>");
var a=NaN,b=null,c=undefined,d=0,e=""; //声明一组变量
var f=5,g="this"; //声明另一组变量
//分别使用声明的各个变量作为判断条件,各变量会自动转换为Boolean型值
if(a){document.write("NaN 转换为Boolean后值为true<br>");}
else{document.write("NaN 转换为Boolean后值为false<br>");}
if(b){document.write("null 转换为Boolean后值为true<br>");}
else{document.write("null 转换为Boolean后值为false<br>");}
if(c){document.write("undefined 转换为Boolean后值为true<br>");}
else{document.write("undefined 转换为Boolean后值为false<br>");}
if(d){document.write("数字 0 转换为Boolean后值为true<br>");}
else{document.write("数字 0 转换为Boolean后值为false<br>");}
if(e){document.write("空字符串 转换为Boolean后值为true<br>");}
else{document.write("空字符串 转换为Boolean后值为false<br>");}
if(f){document.write("数字 5 转换为Boolean后值为true<br>");}
else{document.write("数字 5 转换为Boolean后值为false<br>");}
if(g){document.write("非空字符串 转换为Boolean后值为true");}
else{document.write("非空字符串 转换为Boolean后值为false");}
document.write("<hr>转换为数值型数据:<br>");
//声明几个变量,分别代表不同类型
var a=true,b=false,c=undefined,d=null,e="5";
//通过加1和减1操作,将a转换为数字
document.write("<br>a="+a+";a 转换为数值是:"+(a+1-1));
//通过加1和减1操作,将b转换为数字
document.write("<br>b="+b+";b 转换为数值是:"+(b+1-1));
//通过加1和减1操作,将c转换为数字
document.write("<br>c="+c+";c 转换为数值是:"+(c+1-1));
//通过加1和减1操作,将d转换为数字
document.write("<br>d="+d+";d 转换为数值是:"+(d+1-1));
//通过两次取反,将e转换为数字,其中'"'是单引号包含的一个双引号
//这里不可采用加1减1的方法,因为会转换为字符串
document.write("<br>e="+'"'+e+'"'+";e 转换为数值是:"+(-(-e)));
document.write("<hr>转换为字符串型数据:<br>");
var a=true,b=false,c=undefined,d=null; //声明一组变量
var e=NaN,f=0,g=6; //声明一组变量
//通过使用空字符串与变量连接,将变量转换为字符串
document.write("<br>a="+a+";a 转换为字符串后的值是:"+(""+a));
document.write("<br>b="+b+";b 转换为字符串后的值是:"+(""+b));
document.write("<br>c="+c+";c 转换为字符串后的值是:"+(""+c));
document.write("<br>d="+d+";d 转换为字符串后的值是:"+(""+d));
document.write("<br>e="+e+";e 转换为字符串后的值是:"+(""+e));
document.write("<br>f="+f+";f 转换为字符串后的值是:"+(""+f));
document.write("<br>g="+g+";g 转换为字符串后的值是:"+(""+g));
document.write("</pre>");
//-->
</script>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -