首页 > 其他分享 >JS基础11-3强制类型转换Boolean

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

时间:2022-11-29 16:46:33浏览次数:40  
标签:11 类型转换 console log JS Boolean typeof var true

<!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>

  

标签:11,类型转换,console,log,JS,Boolean,typeof,var,true
From: https://www.cnblogs.com/SadicZhou/p/16935790.html

相关文章