zoukankan      html  css  js  c++  java
  • JS基础_强制类型转换-Boolean

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title></title>
     6         <script type="text/javascript">
     7             
     8             /*
     9              * 将其他的数据类型转换为Boolean
    10              *     - 使用Boolean()函数
    11              *         - 数字 ---> 布尔
    12              *             - 除了0和NaN,其余的都是true
    13              * 
    14              *         - 字符串 ---> 布尔
    15              *             - 除了空串,其余的都是true
    16              * 
    17              *         - null和undefined都会转换为false
    18              * 
    19              *         - 对象也会转换为true
    20              *         
    21              */
    22             
    23             //除了0和NaN,其余的都是true
    24             
    25             var a = 123;  
    26             a = Boolean(a);
    27             console.log(a);//true
    28             console.log(typeof a);//boolean
    29             
    30             
    31             a = -123;     
    32             a = Boolean(a);
    33             console.log(a);//true
    34             console.log(typeof a);//boolean
    35             
    36             
    37             a = Infinity;
    38             a = Boolean(a);
    39             console.log(a);//true
    40             console.log(typeof a);//boolean
    41             
    42             a = 0;        
    43             a = Boolean(a);
    44             console.log(a);//false
    45             console.log(typeof a);//boolean
    46             
    47             
    48             a = NaN;     
    49             a = Boolean(a);
    50             console.log(a);//false
    51             console.log(typeof a);//boolean
    52             
    53             //---------------------------------------------------------------------------------------------------
    54             
    55             //除了空串,其余的都是true
    56             
    57             a = " ";
    58             a = Boolean(a);
    59             console.log(a); //false
    60             console.log(typeof a); //boolean
    61             
    62             a = "abc";
    63             a = Boolean(a);
    64             console.log(a); //true
    65             console.log(typeof a); //boolean
    66             
    67             //-------------------------------------------------------------------------------------
    68             
    69             //null和undefined都会转换为false
    70             
    71             a = null;
    72             a = Boolean(a); 
    73             console.log(a); //false
    74             console.log(typeof a); //boolean
    75             
    76             a = undefined; 
    77             a = Boolean(a);
    78             console.log(a); //false
    79             console.log(typeof a); //boolean
    80             
    81             
    82             
    83         </script>
    84     </head>
    85     <body>
    86     </body>
    87 </html>
  • 相关阅读:
    AD预测论文研读系列2
    hdu 5795
    sg函数的应用
    二分查找
    快速幂
    筛选法素数打表
    多校hdu-5775 Bubble sort(线段树)
    多校hdu5754(博弈)
    多校hdu5738 寻找
    多校hdu5726 线段树+预处理
  • 原文地址:https://www.cnblogs.com/ZHOUVIP/p/7225355.html
Copyright © 2011-2022 走看看