• JavaScript Video Tutorials

JavaScript - The Boolean Object



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);

Here value is an expression to convert into a Boolean object.

It returns an object containing the boolean value.

You can create a boolean primitive in JavaScript by assigning a boolean value to a variable −

let bool = true;

Boolean Properties

Here is a list of the properties of Boolean object −

Sr.No. Property & Description
1 constructor

Returns a reference to the Boolean function that created the object.

2 prototype

The 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

In the example below, we have defined the boolObj variable and stored the boolean object.

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>

Output

typof boolObj == object

JavaScript Boolean() Function

The Boolean() function allows the developer to get the boolean value after evaluating the particular expression passed as a parameter.

Boolean(Expression);

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

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>

Output

Boolean(100 > 90) : true
Boolean(100 < 90) : false
100 == 90 : false

JavaScript Falsy Boolean Values

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

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
All other values are truthy, and the Boolean() function returns true.

Example

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>

Output

Boolean(1) : true
Boolean(-1) : true
Boolean('Hello') : true
Boolean(true) : true
Boolean(10.99) : true
Boolean({name: 'John'}) : true
Boolean(() => {return 1;}) : true
Advertisements