• 【JavaScript的五种基本数据类型及转换】


    js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型就是特殊的(Object)。

    "undefined" 变量未定义
    "boolean" 变量是布尔值
    "string" 变量是字符串
    "number" 变量是数值
    "object" 变量是对象或者null
    "function" 变量是函数

     

     

     

     

    typeof  操作符可以检测变量的数据类型(输出的是一个关于数据类型的字符串)。

    1 //          typeof是得到变量的类型(查看数据类型)
    2             var a;    //undefined
    3             a=null;   //object
    4             a=0;      //number
    5             a=NaN;    //number
    6             a="1";    //string(带引号的基本都是字符串)
    7             a=false;  //boolean布尔
    8             a='';     //string(带引号的基本都是字符串)
    9             alert(typeof a); 

    隐式转换:

    以下是一些例子:

    其中:

    其它类型转换成数值型:
     数值型+undefined=NaN   
    数值型+null=数值
    布尔boolean:true+2=3 false+2=2
     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="utf-8">
     5         <title></title>
     6     <script type="text/javascript">
     7         /*
     8          if(exp){
     9              exp为true的代码段;
    10          }else{
    11              exp为false的代码段;
    12          }
    13          */
    14         //其它类型转换成布尔类型假的有
    15 //        var a;//undefined->false
    16 //      typeof得到变量的类型
    17 //        alert(typeof a);
    18 //             a=null;//null->false
    19 //          0 0.0 NaN->false
    20 //             a=0;
    21 //             a=0.0;
    22 //            a=0/0;
    23 //             a=NaN;
    24             a=null;
    25              alert(a);
    26 //            a='';//空字符串->false
    27 //            a='0';
    28 //            a=' ';
    29 //            alert(typeof a);
    30 //            if(a){
    31 //                alert('真');
    32 //            }else{
    33 //                alert('假');
    34 //            }
    35 //其它类型转换成数值型
    36             var b=undefined;//undefined->NaN
    37             b=null;//null->0
    38             b=false;//true->1
    39 //            b=false;//false->0
    40 //            alert(1+b);    //    数值型+undefined=NaN   数值型+null=数值     ( boolean:true+2=3  false+2=2)
    41             var c='12';//'12'->12
    42 //            c='3king';//'3king'->NaN
    43             c=false;
    44 //            alert(2*c);
    45 //            c='33';
    46 //            alert(typeof c);
    47             c=c*1;
    48 //            alert(typeof c);
    49 
    50             
    51     </script>
    52     </head>
    53     <body>
    54         <h1>隐式转换的例子</h1>
    55         <script type="text/javascript">
    56             //其它类型转换成字符串型
    57             document.write(undefined);//'undefined'
    58             document.write('<br/>');
    59             document.write(null);//'null'
    60             document.write('<br/>');
    61             document.write(NaN);//'NaN'
    62             document.write('<br/>');
    63             document.write(123);//'123'
    64             document.write('<br/>');
    65             document.write(true);//'true'
    66             document.write('<br/>');
    67             document.write(false);//'false'
    68             document.write('<br/>');
    69             //alert(1+"1");//拼接字符串
    70             //alert('2'+12);//拼接字符串
    71         </script>
    72     </body>
    73 </html>

    强制转换:<有五种>

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="utf-8">
     5         <title></title>
     6     <script type="text/javascript">
     7     //其它类型转换成布尔类型false的有
     8         var test=Boolean(0);
     9         test=Boolean(-0);
    10         test=Boolean(NaN);
    11         test=Boolean(undefined);
    12         test=Boolean('');
    13         test=Boolean(0.0);        
    14         test=Boolean('0');
    15         //其它类型转换成字符串型
    16         test=String(1234);
    17         test=String(23.34);
    18         test=String('this is a test');
    19         test=String(true);
    20         test=String(false);
    21         test=String(null);
    22         test=String(undefined);
    23         test=String(NaN);
    24         //其它类型转换成数值型
    25         test=Number(12);
    26         test=Number(232.3);
    27         test=Number(true);
    28         test=Number(false);
    29         test=Number(undefined);
    30         test=Number(NaN);
    31         test=Number(null);
    32         test=Number('3king');
    33         test=Number('324');
    34         //通过parseInt()进行转换成整型
    35         test=parseInt('123');
    36         test=parseInt(222,3);        //234
    37 //        alert(test);
    38         test=parseInt('0xabcdef');    //11295375
    39         //alert(test);
    40         test=parseInt('012344');    //12344
    41 //        alert(test);
    42         test=parseInt(45,16);        //69
    43 //        alert(test);
    44         test=parseInt('3ki23ng');    //3
    45 //        alert(test);                   //3
    46         test=parseInt('true');        //NaN
    47         //alert(test);
    48         test=parseInt(true);        //NaN
    49         //alert(test);
    50         test=parseInt('  35  6 a ');//35
    51 //        alert(test);
    52         //通过parseFloat()转换成浮点型
    53         test=parseFloat('123.34abc');
    54         //test=parseFloat('123');
    55     //    test=parseFloat('sdf');
    56 //        test=parseFloat(' 2e3a');
    57         alert(test);
    58     </script>
    59     </head>
    60     <body>
    61     <h1>强制转换的例子</h1>
    62     </body>
    63 </html>
  • 相关阅读:
    一名中国联络官的来信
    中国女性出席1899年伦敦世界妇女大会
    曾在九江同文任教的中外人士若干
    金韵梅大夫略传
    为何高于四次的方程没有根式解?
    日军进攻九江的影像资料
    美以美会在九江
    九江同文中学与宝洁公司的甘布尔家族
    九江生命活水医院
    微信小程序获取用户信息签名解密C#
  • 原文地址:https://www.cnblogs.com/zxnn/p/8214987.html
Copyright © 2020-2023  润新知