Javascript 简明教程
JavaScript - The Boolean Object
JavaScript Boolean 对象表示两个值,即 “true” 或 “false”。你可以使用 Boolean() 构造函数和 ‘new’ 关键字来创建一个布尔型对象。它将值作为一个参数并返回一个布尔型对象。如果省略了值参数或者它是 0、-0、null、false、NaN、未定义或空字符串 (""),则该对象初始值为 false。在编程中,if-else 语句基于条件表达式的布尔值来评估 ‘if’ 块或 ‘else’ 块的代码。
The JavaScript Boolean object represents two values, either "true" or "false". You can create a boolean object using the Boolean() constructor with a 'new' keyword. It takes a value as parameter and returns a boolean object. If value parameter is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. In programming, the if-else statement evaluates either the code of the 'if' block or the 'else' block based on the boolean value of the conditional expression.
Syntax
使用以下语法来创建布尔型对象。
Use the following syntax to create a boolean object.
const val = new Boolean(value);
此处,value 是要转换为布尔型对象的一个表达式。
Here value is an expression to convert into a Boolean object.
它返回一个包含布尔值的布尔型对象。
It returns an object containing the boolean value.
你可以在 JavaScript 中通过给变量分配布尔值,创建一个布尔原语 −
You can create a boolean primitive in JavaScript by assigning a boolean value to a variable −
let bool = true;
Boolean Properties
以下列出 Boolean 对象的属性:
Here is a list of the properties of Boolean object −
Sr.No. |
Property & Description |
1 |
constructorReturns a reference to the Boolean function that created the object. |
2 |
prototypeThe prototype property allows you to add properties and methods to an object. |
在以下部分,我们将通过几个示例来说明布尔对象的属性。
In the following sections, we will have a few examples to illustrate the properties of Boolean object.
Boolean Methods
以下是布尔对象的方法及其说明的列表。
Here is a list of the methods of Boolean object and their description.
Sr.No. |
Method & Description |
1 |
toSource()Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object. |
2 |
toString()Returns a string of either "true" or "false" depending upon the value of the object. |
3 |
valueOf()Returns the primitive value of the Boolean object. |
在以下部分,我们将通过几个示例演示布尔方法的使用。
In the following sections, we will have a few examples to demonstrate the usage of the Boolean methods.
Example: Creating a Boolean Object
在以下示例中,我们定义了 boolObj 变量并存储了布尔型对象。
In the example below, we have defined the boolObj variable and stored the boolean object.
我们使用了 typeof 运算符来检查 boolObj 变量的类型。在输出中,你可以看到 boolObj 的类型是对象。
We used the typeof operator to check the type boolObj variable. In the output, you can observe that the type of the boolObj is the object.
<html>
<body>
<p id = "output"> </p>
<script>
const boolObj = new Boolean('true'); //defining boolean object
document.getElementById("output").innerHTML = "typof boolObj == " + typeof boolObj;
</script>
</body>
</html>
typof boolObj == object
JavaScript Boolean() Function
Boolean() 函数允许开发人员在评估作为参数传递的特定表达式之后获取布尔值。
The Boolean() function allows the developer to get the boolean value after evaluating the particular expression passed as a parameter.
Boolean(Expression);
value 这里是一个用来评估和获得相关布尔值表达式的表达式。Boolean() 函数基于作为参数传递的表达式返回真或假。
Here value is an expression to evaluate and get related boolean values. The Boolean() function returns the true or false based on the expression passed as a parameter.
Example
在下面的示例中,我们使用了 Boolean() 函数并传递了不同的表达式作为参数。在输出中,你可以观察到 Boolean() 函数返回的布尔值。
In the example below, We used the Boolean() function and passed different expressions as a parameter. In the output, you can observe the boolean value returned by the Boolean() function.
<html>
<body>
<p id = "output"> </p>
<script>
let res = Boolean(100 > 90);
document.getElementById("output").innerHTML = "Boolean(100 > 90) : " + res + "<br>";
res = Boolean(100 < 90);
document.getElementById("output").innerHTML = "Boolean(100 < 90) : " + res + "<br>";
res = 100 == 90;
document.getElementById("output").innerHTML = "100 == 90 : " + res + "<br>";
</script>
</body>
</html>
Boolean(100 > 90) : true
Boolean(100 < 90) : false
100 == 90 : false
JavaScript Falsy Boolean Values
对于假值,Boolean() 函数返回假。有六个假值 - 假、空、未定义、0(零)、""(空字符串)、NaN。
The Boolean() function returns false for the falsy values. There are six falsy values –false, null, undefined, 0 (zero), "" (empty string), NaN.
我们来看一下下面的示例。
Let’s look at the example below.
Example
在下面的代码中,我们将所有假值作为 Boolean() 函数的参数并打印了 Boolean() 函数返回的值。对于所有 7 个值,Boolean() 函数都返回假。
In the code below, we have passed all the falsy values as a parameter of the Boolean() function and printed the returned value from the Boolean() function. The Boolean() function returns false for all 7 values.
<html>
<body>
<p id = "demo"> </p>
<script>
document.getElementById("demo").innerHTML =
"Boolean(0) : " + Boolean(0) + "<br>" +
"Boolean(-0) : " + Boolean(-0) + "<br>" +
"Boolean(null) : " + Boolean(null) + "<br>" +
"Boolean(undefined) : " + Boolean(undefined) + "<br>" +
"Boolean('') : " + Boolean('') + "<br>" +
"Boolean(NaN) : " + Boolean(NaN) + "<br>" +
"Boolean(false) : " + Boolean(false);
</script>
</body>
</html>
Output
Boolean(0) : false
Boolean(-0) : false
Boolean(null) : false
Boolean(undefined) : false
Boolean('') : false
Boolean(NaN) : false
Boolean(false) : false
Example
在下面的代码中,我们将真值作为 Boolean() 函数参数传递了。对于所有真值,Boolean() 函数都返回真。即使我们已经将对象和函数作为 Boolean() 函数参数传递,它仍然返回真。
In the below code, we passed the truthy values as a Boolean() function parameter. The Boolean() function returns true for all truthy values. Even if we have passed the object and function as a Boolean() function parameter, it returns true.
<html>
<body>
<p id = "demo"> </p>
<script>
document.getElementById("demo").innerHTML =
"Boolean(1) : " + Boolean(1) + "<br>" +
"Boolean(-1) : " + Boolean(-1) + "<br>" +
"Boolean('Hello') : " + Boolean('Hello') + "<br>" +
"Boolean(true) : " + Boolean(true) + "<br>" +
"Boolean(10.99) : " + Boolean(10.99) + "<br>" +
"Boolean({name: 'John'}) : " + Boolean({ name: 'John' }) + "<br>" +
"Boolean(() => {return 1;}) : " + Boolean(() => { return 1; });
</script>
</body>
</html>