js将数据类型转化为Boolean类型

<!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>
/*
1.使用 Boolean函数
Number:
除了 NaN和0是false 其余的合法数字都是true
String:
除了空字符串(连一个空格都没有的) false 其余都是true

Undefined 和 null 都是false
*/
/* var a = null;
a = Boolean(a);
console.log(a);
console.log(typeof a); */


/*
2.隐式转换 !!
该方式和 Boolean一致
*/
var a = !!"卡时间段";
console.log(a);
console.log(typeof a);


</script>


</body>
</html>

posted @ 2023-08-15 23:57  进击的程序员3321  阅读(195)  评论(0)    收藏  举报