js学习-day01

转换为Boolean

将其他的数据类型转换为Boolean
  使用boolean()函数

    数字--》布尔

      除了0和NaN,其余的都是true

    字符串--》布尔

      除了空字符串,其余的都是true

    null和undefined都会转换为false

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>转换有Boolean</title>
 5 </head>
 6 <body>
 7     <script type="text/javascript">
 8         var a = 123;//true
 9         a = -123;//true
10         a = 0;//false
11         a = infinity;//true
12         a = NaN;
13 
14         a = "false";//true
15         a = " ";//true
16         a = "";//false
17 
18         a = null;//false
19         a = undefined;//false
20 
21 
22         // 调用Boolean()函数来将a转换为布尔值
23         a = Boolean(a);
24         console.log(typeof a);
25         console.log(a);
26     </script>
27 </body>
28 </html>

猜你喜欢

转载自www.cnblogs.com/lijingjaj/p/11145722.html