<!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>
console.log(Boolean(''));
console.log(Boolean(0));
console.log(Boolean(NaN));
console.log(Boolean(null));
console.log(Boolean(undefined));
console.log(Boolean('小白'));
console.log(Boolean('12'));
/* 总结:转换为布尔型使用Boolean(),在转换时,代表空,否定的值会被转换为false,如空字符串
,0,NaN,null和undefined,其余的值转换为true.
*/
</script>
</body>
</html>
转为布尔型的方法
最新推荐文章于 2023-03-06 16:42:24 发布
本文介绍JavaScript中使用Boolean()函数进行布尔类型转换的规则。详细解释了哪些值会被转换为false,包括空字符串、0、NaN、null及undefined,并说明其他所有值均被转换为true。
352

被折叠的 条评论
为什么被折叠?



