JavaScript Boolean

Boolean values can be represented in JavaScript in two different ways: as primitive boolean values and as Boolean objects. A basic value that denotes either the "true" or "false" state is known as a primitive boolean value. These values are typically applied in logical operators, conditional statements, and other expressions that yield boolean results.

So let's discuss the primitive boolean values first, and then we will cover Boolean objects after that. The Boolean() function is used to create Boolean objects and is also described briefly at the end of this article.

A boolean is a data type in JavaScript that represents the value "true" or "false." It is used to express logical values, which are indispensable in programming for decision-making, conditional execution, and other purposes.

The true and false keywords can be used to create boolean values, or they can result from a comparison or logical operation. For instance:

<!DOCTYPE html>
<html>
<body>

<script>

   // Simulate user authentication
   const isAuthenticated = true;

   // Check if user is logged in
   if (isAuthenticated) {
      console.log("Welcome, user!");
   } else {
      console.log("Please log in to continue.");
   }  

</script>

</body>
</html>

Since "isAuthenticated" is set to "true" the condition if the if statement evaluates to true, which forces the program flow to enter the if's body that executes the console.log() statement that prints the text "Welcome, user!" to the console. Therefore, the following snapshot shows the sample output produced by this example:

javascript boolean example

JavaScript Boolean Object

A Boolean object is a convenient container for a simple boolean value that adds useful features. The Boolean constructor accepts a single argument and returns a new Boolean object; it is used to create Boolean objects. The only values that are converted to false are false, 0, "", null, undefined, and NaN. All other values, including non-empty strings, are converted to true when used as an argument for the Boolean constructor. As an example:

var boolObj = new Boolean(true);

Now the variable "boolObj" can be considered a "Boolean" object.

The primary distinction between a primitive boolean value and a Boolean object is how JavaScript treats them. Primitive boolean values are preferred in most cases because they are faster to create and use and take up less memory than Boolean objects.

Care must be taken when comparing a Boolean object to a primitive boolean value using the strict equality operator (===). Due to the fact that the strict equality operator does not perform type coercion, a Boolean object with the value true is not strictly equal to a primitive boolean value with the same value. Consider the following code demonstrating this concept:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <script>
   
      var boolObj = new Boolean(true);
      console.log(boolObj === true);

      var boolVal = true;
      console.log(boolVal === true);
      
   </script>
   
</body>
</html>
Output
false
true

JavaScript Boolean object properties

The list given below describes the properties of the Boolean object in JavaScript.

JavaScript Boolean object methods

The list given below describes the methods of the Boolean object in JavaScript.

JavaScript Boolean() function

The JavaScript Boolean() function is used to create Boolean objects and to determine whether an expression or variable is true or false. Here's an illustration:

<!DOCTYPE html>
<html>
<body>

   <script>
   
      var x = Boolean(0);
      console.log(x);

      var y = Boolean(1);
      console.log(y);

      var z = Boolean("");
      console.log(z);

      var w = Boolean("hello");
      console.log(w);

      var boolObj = new Boolean(true);
      console.log(boolObj);

      var boolVal = Boolean(true);
      console.log(boolVal);

   </script>
   
</body>
</html>

The output should be:

javascript boolean function example code

The first "console.log()" statement outputs "false", because 0 is falsy, the second outputs "true", because 1 is truthy, the third outputs "false", because an empty string is falsy, and the fourth outputs "true" because a non-empty string is truthy. Whereas the fifth outputs "Boolean {true}," indicating that the "true" is from the boolean object and not the primitive boolean value. Finally, the last, or sixth, console.log statement outputs "true" using Boolean() with "true" as its argument.

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!