123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <!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>
- <script>
- /*
- 将其他的数据类型转换为Boolean
- 方法一:
- -使用Boolean()函数
- 数字-->布尔
- -除了0和NaN,其余的都是true
- 字符串-->布尔
- -除了空串,其余的都是true
- null和undefined都会转换为false
- 对象也会转换为true
- 方法二:
- 对任意的数据类型做两次非运算,即可将其转换为布尔值
- 例:var a = "hello";
- a = !!a;//结果为true
- */
- var a = 123;//true
- a=-123;//true
- a=0;//false
- a=NaN;//false
- a=Infinity;//true
- //调用Boolean()函数来将a转换为布尔值
- a="hello";//true
- a="错误";//true
- a="";//false
- a=null;//false
- a=undefined;//false
- a=Boolean(a);
- console.log(typeof a);
- console.log(a);
- </script>
- </head>
- <body>
-
- </body>
- </html>
|