JavaScript类型转换---隐式转换

JavaScript类型转换—隐式转换

一 转换成布尔类型数据

1.undefined->false
2.nul->false
3.数值型0或0.0或NaN->false
4.字符串长度为0->false
5.其他对象->true
6.undefined->NaN

二 转换为数值型数据

1.null->0
2.true->1|false->0
3.内容为数字->数字,否则转换成NaN
4.其他对象->NaN

三 转换为字符串型数据

1.undefined->“undefined”
2.null->“null”
3.true->“true” false->“false”
4.数值型->NaN,0或者与数值对应的字符串
5.其他对象->如果存在这个对象则转换为toString()方法的值,否则转换为Undefined



<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
        /*
        if(exp){
            exp为true的代码段;
        }else{
         exp为false的代码段;
        }
         */
        //其他类型转换成布尔类型的有如下:
        var a;//undefined->false
        //typeof得到变量的类型
        //alert(typeof a);
        a=null;//null->false
        //0 0.0 NaN->false
        a=0;
        a=0.0;
        a=0/0;//NaN
        a=NaN;
        //alert(a);
        a='';//空字符串->false
        a='0';
        a=' ';
      //  if(a){
        //    alert('真');
       // }else{
         //   alert('假');
        //}
        //其他类型转换为数值型
        var b=1;//undefined->NaN
        b=null;//null->0
        b=true;//true->1
        b=false;//false->0
      //  alert(1+b);
        var c='12';//'12'->12
        c='3king';//'3king->NaN
        c='true';
        //alert(2*c);
        c='33';
        //alert(typeof c);
        //c=c*1;
        //alert(typeof 1*c);
//      其他类型转换为字符串型
    </script>
</head>
<body>
<h1>隐式转换的例子</h1>
<script type="text/javascript">
    //其他类型转换成字符串型
    document.write(undefined);//'undefined'
    document.write('<br/>');
    document.write(null);//'null'
    document.write('<br/>')
    document.write(NaN);//'NaN'
    document.write('<br/>')
    document.write(123);//'123'
    document.write('<br/>')
    document.write(true);//'true'
    document.write('<br/>')
    document.write(false);//'false'
    alert(1+"1");//拼接字符串
    alert('2'+12);//拼接字符串
    alert('1'+'3');
</script>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值