JavaScript | Boolean

Boolean is a datatype that returns either of two values i.e. true or false. In JavaScript, boolean is used as a function to get the value of a variable, object, conditions, expressions etc. in terms of true or false.
Example of boolean values:
Here a1 and a2 stores the boolean value i.e. true and false respectively.

var a1 = true;
var a2 = false;

Note: Below variables are initialized with strings not boolean values.

var a1 ="true";
var a2 ="false";

Boolean() function in JavaScript: Boolean function returns the boolean value of variable. It can also be used to find boolean result of a condition, expression etc.

Syntax:

Boolean(variable/expression) 

Note: A variable or object which has value are treated as true boolean values. ‘0’, ‘NaN’, empty string, ‘undefined’, ‘null’ are treated as false boolean values.



JavaScripts to show the working of boolean values

Code #1:
Below program will give true values as output.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  <body>
     <script>
        document.write('Boolean(10) is ' + Boolean(10));
        document.write('<br>');
        document.write('Boolean("GeeksforGeeks") is ' + Boolean("GeeksforGeeks"));
        document.write('<br>');
        document.write('Boolean(2.74) is ' + Boolean(2.74));
        document.write('<br>');
        document.write('Boolean(-1) is ' + Boolean(-1));
        document.write('<br>');
        document.write("Boolean('true') is " + Boolean('true'));
        document.write('<br>');
        document.write("Boolean('false') is " + Boolean('false'));
        document.write('<br>');
        document.write('Boolean(3 * 2 + 1.11) is ' + Boolean(3 * 2 + 1.11));
        document.write('<br>');
        document.write('Boolean(1<2) is ' + Boolean(1 < 2));
          
     </script>
  </body>
</html>    

chevron_right


Output:

Boolean(10) is true
Boolean("GeeksforGeeks") is true
Boolean(2.74) is true
Boolean(-1) is true
Boolean('true') is true
Boolean('false') is true
Boolean(3 * 2 + 1.11) is true
Boolean(1<2) is true

Code #2:
Below program will give false values as output.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script>
        var e; //undefined
        document.write('Boolean(0) is ' + Boolean(0));
        document.write('<br>');
        document.write('Boolean("") is ' + Boolean(""));
        document.write('<br>');
        document.write('Boolean(e) undefined is ' + Boolean(e));
        document.write('<br>');
        document.write('Boolean(-0) is ' + Boolean(-0));
        document.write('<br>');
        document.write('Boolean(false) is ' + Boolean(false));
        document.write('<br>');
        document.write('Boolean(NaN) is ' + Boolean(NaN));
        document.write('<br>');
        document.write('Boolean(null) is ' + Boolean(null));
        document.write('<br>');
        document.write('Boolean(1>2) is ' + Boolean(1 > 2));
    </script>
</body>
  
</html>             

chevron_right


Output:

Boolean(0) is false
Boolean("") is false
Boolean(e) undefined is false
Boolean(-0) is false
Boolean(false) is false
Boolean(NaN) is false
Boolean(null) is false
Boolean(1>2) is false

JavaScript Boolean object:
The boolean object in javascript is an object wrapper for boolean values. Booleans in JavaScript can also be defined using the new keyword.
Syntax:

new Boolean(value)

Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script>
        var v1 = false;
        var v2 = new Boolean(false);
        var v3 = new Boolean("");
        var v4 = new Boolean(0);
        var v5 = new Boolean(true);
        var v6 = new Boolean("GeeksforGeeks");
        document.write('v1 = ' + v1);
        document.write("<br>");
        document.write('v2 = ' + v2);
        document.write("<br>");
        document.write('v3 = ' + v3);
        document.write("<br>");
        document.write('v4 = ' + v4);
        document.write("<br>");
        document.write('v5 = ' + v5);
        document.write("<br>");
        document.write('v6 = ' + v6);
    </script>
</body>
</html>

chevron_right


Output:

v1 = false
v2 = false
v3 = false
v4 = false
v5 = true
v6 = true

Code #4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script>
        var v1 = true;
        var v2 = new Boolean(true);
  
        document.write('v1 = = v2 is ' + (v1 == v2));
        document.write("<br>");
        document.write('v1 = = = v2 is ' + (v1 === v2));
    </script>
</body>
</html>

chevron_right


Output:

v1 = = v2 is true
v1 = = = v2 is false

Note: note that v1 = = = v2 is not true as the type of v1 and v2(object) is not same.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.