Boolean Values in JavaScript

In JavaScript, a variable of Boolean type can store one of the two values including True and False. There are two keywords which are reserved for representing these values.

  • true
  • false

Most of the time, boolean values are used as a result of an expression. For example,

  • 5 > 1
    Result = true.
  • 10 > 100
    Result = false.

However, boolean values come into action when the code has to deal with the control structures, where the flow of control is directed to a particular section of the code with the help of boolean values and decision making statements like if, if-else etc.

How to create a Boolean type variable in JavaScript?

Apart from acting as a result of an expression, boolean values can also be assigned to a variable using the assignment operator, i.e. '='.

Example:

<!DOCTYPE html>
<html lang="en">
 <head> </head>
 <body>
  <script>
   var x = true; 
   document.write("x = ", x);
   document.write("Datatype: ", typeof(x)); //detecting the datatype of x.
   var y = 10<5; 
   document.write("y = ", y);
   document.write("Datatype: ", typeof(y)); //detecting the datatype of y.
  </script>
 </body>
</html>

Output:

x = true
Datatype: boolean

y = false
Datatype: boolean

In the above code, there are two variables created, namely, x and y.
Variable x holds the value true directly, whereas variable y holds the result of the expression '10<5,' i.e. false.

JavaScript Booleans as Objects

A boolean value can also be created as an Object with the help of the keyword new.

Syntax:
var variable_name = new Boolean(value)

Example:

<!DOCTYPE html>
<html lang="en">
 <head> </head>
 <body>
  <script>
   var result = new Boolean(true);
   document.write("result = ", result);
  </script>
 </body>
</html>

Output:

result = true

Built-in function Boolean() in JavaScript

JavaScript provides a function Boolean() which shows the boolean value stored in a variable. The variable name is given to the function as the parameter.

Syntax:
Boolean(variable_name)

Example:
Let us take the different type of values and find out the boolean value stored in them.

<!DOCTYPE html>
<html lang="en">
 <head> </head>
 <body>
  <script>
   var a = 10<5; //comparison
   document.write("Boolean(a) = ", Boolean(a), "<br>");
   var b = 12345; //number
   document.write("Boolean(b) = ", Boolean(b), "<br>");
   var c = "Good Day!" //string
   document.write("Boolean(c) = ",Boolean(c), "<br>");
   var d = 0; 
   document.write("Boolean(d) = ", Boolean(d), "<br>");
   var e = null; 
   document.write("Boolean(e) = ", Boolean(e), "<br>");
   var f = undefined; 
   document.write("Boolean(f) = ", Boolean(f), "<br>");
   var g = Infinity; 
   document.write("Boolean(g) = ", Boolean(g), "<br>");
   var h = NaN; 
   document.write("Boolean(h) = ", Boolean(h), "<br>");
  </script>
 </body>
</html>

Output:

Boolean(a) = false
Boolean(b) = true
Boolean(c) = true
Boolean(d) = false
Boolean(e) = false
Boolean(f) = false
Boolean(g) = true
Boolean(h) = false

With the above example, you can conclude that a variable holding a value, be it a numberĀ or a string, will always be true, whereas a variable that stores nothing (i.e. null or zero or undefined or NaN) is false.