JavaScript Boolean() Constructor

0
4

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

LEAVE A REPLY

Please enter your comment!
Please enter your name here