JavaScript 布尔值代表两个值之一:true 或 假。
很多时候,在编程中,您需要一种只能有一个的数据类型 有两个值,例如
是/否
开关
真假
为此,JavaScript 有一个布尔数据类型。它只能 取值true或false。
您可以使用 Boolean()
函数来查明表达式(或变量)是否为 真的:
Boolean(10 > 9)
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the value of Boolean(10 > 9):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Boolean(10 > 9);
</script>
</body>
</html>
或者更简单:
(10 > 9)
10 > 9
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the value of 10 > 9:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 10 > 9;
</script>
</body>
</html>
JS 比较一章全面概述了比较运算符。
JS 条件一章给出了条件语句的完整概述。
这里有些例子:
==
描述:等于
示例:
if (day == "Monday")
>
描述:大于
示例:
if (salary > 9000)
<
描述:小于
示例:
if (age < 18)
表达式的布尔值是所有 JavaScript 比较和条件的基础。
100
3.14
-15
"Hello"
"false"
7 + 1 + 3.14
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"100 is " + Boolean(100) + "<br>" +
"3.14 is " + Boolean(3.14) + "<br>" +
"-15 is " + Boolean(-15) + "<br>" +
"Any (not empty) string is " + Boolean("Hello") + "<br>" +
"Even the string 'false' is " + Boolean('false') + "<br>" +
"Any expression (except zero) is " + Boolean(1 + 7 + 3.14);
</script>
</body>
</html>
0(零)的布尔值为false:
let x = 0;
Boolean(x);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of 0:</p>
<p id="demo"></p>
<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
</body>
</html>
-0(负零)的布尔值为false:
let x = -0;
Boolean(x);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of -0:</p>
<p id="demo"></p>
<script>
let x = -0;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
</body>
</html>
"" (空字符串)的布尔值为false:
let x = "";
Boolean(x);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of "":</p>
<p id="demo"></p>
<script>
let x = "";
document.getElementById("demo").innerHTML = Boolean("");
</script>
</body>
</html>
undefined 的布尔值为 false:
let x;
Boolean(x);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of undefined:</p>
<p id="demo"></p>
<script>
let x;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
</body>
</html>
null 的布尔值为 false:
let x = null;
Boolean(x);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of null:</p>
<p id="demo"></p>
<script>
let x = null;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
</body>
</html>
false 的布尔值是(你猜对了)false:
let x = false;
Boolean(x);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of false:</p>
<p id="demo"></p>
<script>
let x = false;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
</body>
</html>
NaN 的布尔值为 false:
let x = 10 / "Hallo";
Boolean(x);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of NaN:</p>
<p id="demo"></p>
<script>
let x = 10 / "Hello";
document.getElementById("demo").innerHTML = Boolean(x);
</script>
</body>
</html>
通常 JavaScript 布尔值是从文字创建的原始值:
let x = false;
但布尔值也可以使用关键字 new
定义为对象:
let y = new Boolean(false);
let x = false;
let y = new Boolean(false);
//
typeof x returns boolean
//
typeof y returns object
不要创建布尔对象。
new
关键字使代码变得复杂并降低执行速度。
布尔对象可能会产生意想不到的结果:
使用 ==
运算符时,x 和 y 相等:
let x = false;
let y = new Boolean(false);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared:</p>
<p id="demo"></p>
<script>
let x = false; // x is a boolean
let y = new Boolean(false); // y is an object
document.getElementById("demo").innerHTML = (x==y);
</script>
</body>
</html>
使用 ===
运算符时,x 和 y 不等于:
let x = false;
let y = new Boolean(false);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared:</p>
<p id="demo"></p>
<script>
let x = false; // x is a Boolean
let y = new Boolean(false); // y is an object
document.getElementById("demo").innerHTML = (x===y);
</script>
</body>
</html>
请注意 (x==y) 和 (x===y) 之间的区别。
(x == y)
是真是假?
let x = new Boolean(false);
let y = new Boolean(false);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared.</p>
<p id="demo"></p>
<script>
const x = new Boolean(false);
const y = new Boolean(false);
document.getElementById("demo").innerHTML = (x==y);
</script>
</body>
</html>
(x === y)
是真是假?
let x = new Boolean(false);
let y = new Boolean(false);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared.</p>
<p id="demo"></p>
<script>
const x = new Boolean(false);
const y = new Boolean(false);
document.getElementById("demo").innerHTML = (x===y);
</script>
</body>
</html>
比较两个 JavaScript 对象总是返回false。
如需完整参考,请访问我们的完整版 JavaScript 布尔参考。
该参考包含所有布尔属性和方法的描述和示例。