1.JavaScript 布尔(逻辑)代表两个值之一:true 或 false。
2.使用Boolean() 函数来确定表达式(或变量)是否为真:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js逻辑</title>
</head>
<body>
<p>显示 Boolean(10 > 9) 的值:</p>
<button onclick="myFunction()">显示</button>
<p id="demo"></p>
<script>
function myFunction(){
document.getElementById("demo").innerHTML=Boolean(10>9);
}
</script>
</body>
</html>
显示效果为:
3.所有不具有“真实”值的即为 False,如:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js逻辑</title>
</head>
<body>
<p>显示布尔值:</p>
<button onclick="myFunction()">显示</button>
<p id="demo"></p>
<script>
function myFunction(){
var x;
var b1 = Boolean(0);
var b2 = Boolean(-0);
var b3 = Boolean(x);
var b4 = Boolean("");
var b5 = Boolean(false);
var b6 = Boolean(null);
document.getElementById("demo").innerHTML =
"0是 " + b1 + "<br>" +
"-0是 " + b2 + "<br>" +
"undefined是 " + b3 + "<br>" +
"空值是 " + b4 + "<br>" +
"false也是 " + b5 + "<br>" +
"null是" + b6;
}
</script>
</body>
</html>
显示效果为: