Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Boolean

Improve Article
Save Article
  • Last Updated : 02 Jan, 2023
Improve Article
Save Article

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.

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

Here a1 and a2 store 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 the variable. It can also be used to find the boolean result of a condition, expression, etc.

Syntax:  

Boolean(variable/expression) 

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)

Below are examples of the JavaScript Boolean method.

Example 1: Below program will give true values as output

javascript




function gfg() { 
    console.log(Boolean(12));
gfg();

Output: 

true

Example 2:

Javascript




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

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

Example 3: Below program will give false values as output.  

javascript




var e; //undefined
console.log('Boolean(0) is ' + Boolean(0));
console.log('Boolean("") is ' + Boolean(""));
console.log('Boolean(e) undefined is ' 
             + Boolean(e));
console.log('Boolean(-0) is ' + Boolean(-0));
console.log('Boolean(false) is ' + Boolean(false));
console.log('Boolean(NaN) is ' + Boolean(NaN));
console.log('Boolean(null) is ' + Boolean(null));
console.log('Boolean(1>2) is ' + Boolean(1 > 2));

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

Example 4:  

javascript




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('v2 = ' + v2);
document.write('v3 = ' + v3);
document.write('v4 = ' + v4);
document.write('v5 = ' + v5);
document.write('v6 = ' + v6);

Output:  

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

Example 5:  

javascript




var v1 = true;
var v2 = new Boolean(true);
  
console.log('v1 = = v2 is ' + (v1 == v2));
console.log('v1 = = = v2 is ' + (v1 === v2));

Output: 

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

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

Supported Browsers:

  • Google Chrome 6 and above
  • Edge 12 and above
  • Firefox 4 and above
  • Internet Explorer 9 and above
  • Opera 12 and above
  • Safari 5.1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!