JavaScript Boolean
Last Updated :
30 Jul, 2024
JavaScript Boolean represents true or false values. It’s used for logical operations, condition testing, and variable assignments based on conditions. Values like 0, NaN, empty strings, undefined, and null are false; non-empty strings, numbers other than 0, objects, and arrays are true.
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: The below variables are initialized with strings, not boolean values.
let a1 ="true";
let a2 ="false";
Boolean() function in JavaScript
The Boolean() function in JavaScript converts any value to its corresponding Boolean representation: truthy values become true, and falsy values become false.
Syntax:
Boolean(variable/expression)
Example 1: The below program will give true values as output.
javascript
function gfg() {
console.log(Boolean(12));
}
gfg();
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));
OutputBoolean(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));
OutputBoolean(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)
Below are examples of the JavaScript Boolean method.
Example 1: 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);
Outputv1 = false
v2 = false
v3 = false
v4 = false
v5 = true
v6 = true
Example 2: 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));
Outputv1 = = 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
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.
JavaScript Boolean – FAQs
What is a Boolean in JavaScript?
A Boolean is a primitive data type in JavaScript that can have one of two values: true or false. It is used to represent logical values and control the flow of the program.
What values are considered truthy or falsy in JavaScript?
- Falsy values: false, 0, -0, 0n, “” (empty string), null, undefined, NaN.
- Truthy values: All values that are not falsy, including objects, non-zero numbers, non-empty strings, and arrays.
How do you use Booleans in conditional statements?
Booleans are commonly used in conditional statements like if, else, while, and for loops to control the flow of the program.
How do you compare Boolean values?
You can compare Boolean values using standard comparison operators (==, !=, ===, !==). The strict equality operators (===, !==) are recommended to avoid type coercion.
How do Boolean objects differ from Boolean primitives?
Boolean objects are created using the Boolean constructor and are objects, while Boolean primitives are simply true or false. Boolean objects are always truthy, even if they represent false.