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.
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.
let a1 = true; let a2 = false;
Note: Below variables are initialized with strings, not boolean values.
let a1 ="true"; let 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: Below program will give true values as output.
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
let 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: Below program will give false values for the first 4 variables & true for last 2 values as output.
javascript
let v1 = false ; let v2 = new Boolean( false ); let v3 = new Boolean( "" ); let v4 = new Boolean(0); let v5 = new Boolean( true ); let v6 = new Boolean( "GeeksforGeeks" ); console.log( 'v1 = ' + v1); console.log( 'v2 = ' + v2); console.log( 'v3 = ' + v3); console.log( 'v4 = ' + v4); console.log( 'v5 = ' + v5); console.log( 'v6 = ' + v6); |
Output:
v1 = false v2 = false v3 = false v4 = false v5 = true v6 = true
Example 5: Below program will give true for the first value & false for the second value as output.
javascript
let v1 = true ; let 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
- 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.
Please Login to comment...