您好,欢迎访问一九零五行业门户网

javascript隐式转换详解

javascript是web前端开发的必学技术,今天和大家分享的就是javascript的基础知识隐式转换,希望可以帮助大家更好的学习。
转换成布尔类型假
undefined->false
null->false
数值型0或0.0或nan->false
字符串长度为0->false
其它对象->true
<html> <head> <meat charset=”utf-8”> <title></title> <script type=”text/javascript”> a=null; a=0; a=0.0; a=0/0;//nan a=’’; a=’0’; a=’’; if(a){ alert(‘真’); } else{ alert(’假’); } </script> <body> <隐式转换例子> </body> </html>
转换为数值型数据
undefined->nan
null->0
true->1|false->0
内容为数字->数字,否则转换成nan
其它对象->nan
<html> <head> <meat charset=”utf-8”> <title></title> <script type=”text/javascript”> a=null; a=0; a=0.0; a=0/0;//nan a=’’; a=’0’; a=’’; if(a){ alert(‘真’); } else{ alert(’假’); } var b=undefined; b=null; b=true; b=false; var c=’12’; c=’3king; c=’true’; c=’33’; alert(typeof c); c=c*1; alert(typeof c); </script> <body> <隐式转换例子> </body> </html>
转换为字符串型数据
undefined->"undefined"
null->"nan"
true->"true" false->"false"
数值型->nan、0或者与数值对应的字符串
其它对象->如果存在这个对象则转换为tostring()方法的值,否则转换为undefined
<html> <head> <meat charset=”utf-8”> <title></title> <body> <script type=”text/javascript”> document.write(undefined); document.write(‘<br>’); document.write(null); document.write(‘<br>’); document.write(nan) document.write(‘<br>’); document.write123l); document.write(‘<br>’); document.write(true); document.write(‘<br>’); document.write(false); document.write(‘<br>’); alert(1+”1”); alert(‘2’+”12”); </script> </body> </html>
运行结果:
其它类似信息

推荐信息