Tutorialspoint.dev

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.

<!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>    

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.

<!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>             

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:

<!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>

Output:

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

Code #4:

<!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>

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.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter