灵心如玉,守一生无惧|

SadicZhou

园龄:3年2个月粉丝:7关注:4

JS基础11-3强制类型转换Boolean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <script>
        /*
           将其他类型转化为Boolean
           -使用Boolean()函数
            数字-》布尔:除了0 和 NAN都是true
            字符串-》布尔:除了空串其余都是true
            null和undefined都是false
            对象也会转换为true
        */
        //   数字-》布尔:除了0 和 NAN都是true
        var a = 123
        a = Boolean(a)//true
        console.log(a, typeof (a))
        var b = -123
        b = Boolean(b)//true
        console.log(b, typeof (b))
        var c = 0
        c = Boolean(c)//false
        console.log(c, typeof (c))
        var d = NaN
        d = Boolean(d)//false
        console.log(d, typeof (d))
        //字符串类型转布尔值
        var a2 = 'hello'
        a2 = Boolean(a2)
        console.log(a2, typeof (a2))//true
        var b2 = ''
        b2 = Boolean(b2)
        console.log(b2, typeof (b2))//false
        var c3 = 'true'
        c3 = Boolean(c3)
        console.log(c3, typeof (c3))//true
        var d3 = 'false'
        d3 = Boolean(d3)
        console.log(d3, typeof (d3))//true
        var e3 = ' '//字符串里面是空格
        d3 = Boolean(e3)
        console.log(d3, typeof (d3))//true
        //null和undefined转布尔
        var a4 = null
        a4 = Boolean(a4)
        console.log(a4, typeof (a4))//false
        var b4 = undefined
        b4 = Boolean(b4)
        console.log(b4, typeof (b4))//false
    </script>
</body>
 
</html>

  

本文作者:SadicZhou

本文链接:https://www.cnblogs.com/SadicZhou/p/16935790.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   SadicZhou  阅读(18)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 尚好的青春 孙燕姿
  2. 2 孙燕姿
  3. 3 克卜勒 孙燕姿
- 孙燕姿
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.