The Wayback Machine - https://web.archive.org/web/20240712050630/https://www.geeksforgeeks.org/javascript-boolean-constructor/
Open In App

JavaScript Boolean() Constructor

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Boolean() constructor in JavaScript is a built-in function that can be used to create a new Boolean object. The Boolean() constructor takes a single argument and returns a Boolean object with a value of true or false depending on the type and value of the argument passed in. 

Syntax:

Boolean(value)

Parameter: This constructor accepts a single argument 

  • value: The parameter contains the value of the boolean

Below are examples of the Boolean Constructor property.

Example 1: In this example, we will create simple boolean values

javascript




function func() {
    let value1 = Boolean(true);
    let value2 = Boolean(false);
    console.log(value1);
    console.log(value2);
}
func();


Output:

true
false

Example 2: In this example, we will convert String, Number, and Date to boolean data type using the Boolean constructor as a function

javascript




function func() {
    let value1 = Boolean("Hello");
    let value2 = Boolean("0");
    let value3 = Boolean(0);
    let value4 = Boolean(new Date());
    console.log(value1);
    console.log(value2);
    console.log(value3);
    console.log(value4);
}
func();


Output:

true
true
false
true

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Safari
  • Opera

We have a complete list of Javascript Boolean methods, to check those please go through the Javascript Boolean Complete Reference article.


Similar Reads

Explain non-boolean value coercion to a boolean one in JavaScript
As we all know javascript is a forgiving language. It does not mind silly mistakes which programmers do. So, sometimes it gives unexpected results. So it senses Javascript says "I can do every possibility". Coercion is one of those in which javascript gives weird results because javascript automatically performs type conversion in which first value
3 min read
JavaScript Boolean Constructor Property
JavaScript boolean constructor property returns the constructor function for an object. The function which is returned by this property is just the reference to this function, not a Boolean containing the function’s name The JavaScript number constructor, string constructor, and boolean constructor return function Boolean() { [native code] }, funct
1 min read
JavaScript Boolean valueOf() Method
The boolean.valueOf() method is used to return a boolean value either "true" or "false" depending upon the value of the specified boolean object. Syntax: boolean.valueOf() Parameter: This method does not accept any parameter. Return value: It returns a boolean value either "true" or "false" depending upon the value of the specified boolean object.
4 min read
JavaScript Boolean toString() Method
The boolean.toString() method is used to return a string either "true" or "false" depending upon the value of the specified boolean object. Syntax: boolean.toString() Parameter: This method does not accept any parameter. Return Values: It returns a string either "true" or "false" depending upon the value of the specified boolean object. Below are e
3 min read
Convert boolean result into number/integer in JavaScript
A JavaScript boolean represents one of two values: true or false. However, if one wants to convert a variable that stores boolean value, into integer "0" or "1", they can do so using multiple approaches. We will look into some of them in this article. The most popular methods are: Using ternary or conditional operator Using unary + operator. Using
3 min read
JavaScript Boolean Reference
JavaScript Boolean is a datatype that returns either true or false In JavaScript, a boolean is used as a function to get the value of a variable, object, conditions, expressions, etc in terms of true or false Syntax: Boolean(variable/expression) Example: If the operands are equal, the equal to operator == returns true C/C++ Code <script> cons
2 min read
How to generate a random boolean using JavaScript ?
The task is to generate random boolean value. Here we are going to use JavaScript to achieve the goal. Approach 1: Calculate Math.random() function. If it is less than 0.5, then true otherwise false. Example 1: This example implements the above approach. <!DOCTYPE HTML> <html> <head> <title> How to generate a random boolean
2 min read
Implement a Function that Accept Array and Condition and Returns Boolean Values in JavaScript
In JavaScript language every function is a special function that checks the element of the array with the condition given in the input, Each element is checked against the condition, and if the condition is satisfied then every function returns the true result else if any of the element of the array fails against the condition then the false result
2 min read
How to initialize a boolean array in JavaScript ?
The boolean array is a sequence of variables that can only carry the values true or false. A Boolean value can only be true or false and cannot have any other intermediate value. An array is a collection of data types that are stored at numerical places in a linear memory space. True and false values are defined by the JavaScript standard as a dist
3 min read
How to convert Number to Boolean in JavaScript ?
We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer “0” or “1” into Boolean Value i.e. "false" or "true".  Below are the approaches: Table of Content Boolean() methodU
2 min read
three90RightbarBannerImg