14.14.尚硅谷_JS基础_转换为Boolean
将其他的数据类型转换为 Boolean:
1、数字转换为布尔值:
除了 ==0 和 NaN== .其余的都是true。
2、字符串转换为布尔值:
除了==空串( a = "";)==,其余的都是true。
3、 null 和 undefined都会转换为false。
4、对象(object)也会转换为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>
<script>
// 将其他的数据类型转换为 Boolean
// - 使用 Boolean()函数
// - 数字 ---> 布尔
// - 除了 0 和 NaN .其余的都是true。
// - 字符串 ---> 布尔
// - 除了空串( a = "";),其余的都是true。
// - null 和 undefined都会转换为false。
// - 对象也会转换为true.
var a = 123;//true;
a = -123;//true;
a = 0;//false;
a = Infinity;//true;
a = NaN;//false;
//调用 Boolean()函数来将a转换为布尔值。
a = Boolean(a)
a = "";//空串
a = null;//没有
a = undefined;//未定义
a = Object;//对象
a = Boolean(a)
console.log(typeof a)
console.log(a)
</script>
</head>
<body>
</body>
</html>