非常简单:使用操作符 !!
示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var a = 10;
var b = "aaa"
var c = "";
var d;
document.write("a 转换为bool:" + !!a + "</br>")
document.write("b 转换为bool:" + !!b + "</br>")
document.write("c 转换为bool:" + !!c + "</br>")
document.write("d 转换为bool:" + !!d + "</br>")
</script>
</head>
<body>
</body>
</html>
输出结果:
a 转换为bool:true
b 转换为bool:true
c 转换为bool:false
d 转换为bool:false