JavaScript Boolean Reference Manual
JavaScript Boolean (Boolean)
JavaScript boolean values can have one of two values: true or false.
Boolean() Function
You can use the Boolean() function to determine if an expression is true:
Example
Boolean(10 > 9) // Returns true
Or even simpler:
Example
(10 > 9) // Returns true 10 > 9 // Also returns true
For tutorials on boolean values, please read our JavaScript Boolean Tutorial.
Boolean Properties
Properties | Description |
---|---|
constructor | Returns the function that creates the JavaScript Boolean prototype. |
prototype | Allow you to add properties and methods to the Boolean prototype. |
Boolean Methods
Method | Description |
---|---|
toString() | Converts the boolean value to a string and returns the result. |
valueOf() | Returns the original value of the boolean value. |
Boolean Object
The Boolean object represents two values: "true" or "false".
Syntax for creating Boolean objects:
new Boolean(value); // Constructor Boolean(value); // Conversion function
Parameter
Parameter value The value stored by the Boolean object or the value to be converted to a boolean value.
Return Value
When called as a constructor (with the operator new), Boolean() will convert its parameter into a boolean value and return a Boolean object containing that value.
If called as a function (without the operator new), Boolean() will only convert its parameter into a primitive boolean value and return this value.
Note:If the value parameter is omitted, or set to 0, -0, null, "", false, undefined, or NaN, the object is set to false. Otherwise, it is set to true (even if the value parameter is the string "false").
Description of Boolean Object
In JavaScript, boolean is a basic data type. The Boolean object is a boolean object that packages boolean values.
When the toString() method is called to convert a boolean value to a string (usually called implicitly by JavaScript), JavaScript will inherently convert this boolean value into a temporary Boolean object, and then call the toString() method of this object.
Supplementary Reading
For more information, please read the relevant content in the Advanced JavaScript Tutorial:
- ECMAScript Reference Types
- Reference types are usually called classes (class) or objects. This section explains the predefined reference types in ECMAScript.