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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步