<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/*
将其他的数据类型转换为Boolean
-使用Boolean()函数
--数字--->布尔
-除了0和NaN,其余都是true
字符串---.>
除了空串,其余都是true
null和undefined都会转换为falase
对象也会转换为true
*/
var a=256;//true
a=-256;//true
a=0;//true
a=Infinity;
a=NaN;
//调用Boolean()函数来将a转换为布尔值
a=Boolean(a);
a="null";//false
a=Boolean(a);
a=undefined;
a=Boolean(a);
console.log(typeof a);
console.log(a);
</script>
</head>
<body>
</body>
</html>
使用Boolean()函数除了0和NaN其余都是true
var a=256;//true
a=-256;//true
a=0;//true
null和undefined都会转换为falase尾部的代码结果如下图

这篇博客探讨了JavaScript中使用Boolean()函数进行数据类型转换的规则。文章指出,除0和NaN外,所有数字都会转换为true,空字符串会转换为false,而null和undefined则转换为false。此外,对象也会被转换为true。示例代码展示了这些转换的过程,并通过console.log输出了变量的类型和转换结果。
1648

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



