Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Boolean Data Type in JavaScript

The boolean is a primitive data type in JavaScript. It represents a simple true/false value. They are implemented as numerical values with a single binary digit (i.e., 0 & 1). The Boolean is an object wrapper for the primitive boolean value. Also, let us find the difference between Boolean object vs boolean primitive. The JavaScript also has a Boolean global function, which we use to convert any value to boolean primitive type. We also learn how to convert boolean to string and string to boolean.

Defining boolean

There are two ways you can create the primitive boolean variable. using the normal variable declaration or using the global Boolean function.

boolean declaration

The following example creates two primitive boolean variables. You can see that the typeof returns as boolean.

Example

                            
let isDone= true;          // primitive boolean type
let isPending= false;      // primitive boolean type
 
console.log(isDone);                //output true
console.log(isPending);             //output false
 
console.log(typeof(isDone));       //output boolean
 
 
**** output ****
true
false
boolean
 
                            
                        
                            
                        

Boolean Global function

The Boolean() global function converts its argument to a primitive boolean value and returns it.

                            
let boolvar=Boolean(true)
console.log(boolvar)           //true
console.log(typeof(boolvar))   //boolean
 
**** output ****
true
boolean
 
                            
                        
                            
                        

You can pass any value to the Boolean function. It always converts them to either true or false. Whether a particular value converts to true or false depends on whether the value is truthy or falsy.

Truthy & Falsy

We can convert any types to boolean in JavaScript using the Boolean function

Falsy are those values, that convert to boolean false. There are eight falsy values in JavaScript. They are false, 0 (zero), -0 (minus zero) , 0n (BigInt zero) , " " (empty string), null, undefined & NaN.

Everything else converts to true, hence we call them as Truthy.

Examples of Falsy

                            
console.log(boolvar)           
 
 boolvar=Boolean(0)             // 0 is false
 console.log(boolvar)           
  
 boolvar=Boolean(-0)            // -0 is also false
 console.log(boolvar)           
  
 boolvar=Boolean(0n)            //BigInt 0 is also false
 console.log(boolvar)           
  
 boolvar=Boolean("")            //empty string is false
 console.log(boolvar)           
  
 boolvar=Boolean(null)          //null is also false
 console.log(boolvar)           
  
 boolvar=Boolean(undefined)     //undefined is also false
 console.log(boolvar)           
  
 boolvar=Boolean(NaN)           //NaN is also false 
 console.log(boolvar)          
  
 
                            
                        
                            
                        

Boolean Object

The Boolean is an object and is a wrapper around boolean.primitive type. You can create a Boolean object using the constructor function.

                            
let boolVar = new Boolean("test");
console.log(boolVar);
console.log(typeof(boolVar));
 
 
***output***
Boolean {true}
object
 
 console.log(boolvar)          
  
 
                            
                        
                            
                        

As shown in the previous example, zero, empty string, null, undefined results in false. Everything else returns true.

You can use thevalueOf method to get the primitive boolean back.

                            
let boolobj=new Boolean("test")
console.log(boolobj)                         //[Boolean: true]
console.log(typeof(boolobj))                 //object
 
 
console.log(boolobj.valueOf())               //true
console.log(typeof(boolobj.valueOf()))       //boolean
    
  
 
                            
                        
                            
                        

The following code is similar to the above, except we used the primitive boolean instead of object.

                            
var b  = false;
console.log(b.valueOf())     //false
if (b) {
  console.log("b is true although b is false");     //
}
else {
    console.log("b is false");     //
}
 
 
// false
// b is false
    
  
 
                            
                        
                            
                        

Boolean vs boolean

The boolean is a primitive type and Boolean is an object. Boolean() is a global function

The boolean is created using variable declaration or using the Boolean() function. The Boolean object is created using the constructor (using the new Boolean())

And since the Boolean object is an object, comparing it with boolean value always results in true, irrespective of its internal value.

                            

                            var b  = new Boolean(false);
console.log(b.valueOf())     //false
if (b) {
  console.log("b is true although b is false");     //
}
else {
    console.log("b is false");     //
}
 
 
// false
// b is true although b is false     
  
 
                            
                        
                            
                        

The following code is similar to the above, except we used the primitive boolean instead of object.

                            
var b  = false;
console.log(b.valueOf())     //false
if (b) {
  console.log("b is true although b is false");     //
}
else {
    console.log("b is false");     //
}
 
 
// false
// b is false
    
  
 
                            
                        
                            
                        

The following code is similar to the above, except we used the primitive boolean instead of object.

                            
var b  = false;
console.log(b.valueOf())     //false
if (b) {
  console.log("b is true although b is false");     //
}
else {
    console.log("b is false");     //
}
 
 
// false
// b is false
    
  
 
                            
                        
                            
                        

Convert boolean to number

Converting the boolean to the number primitive will result in 1 for true and 0 for false.

                            
let trueNum=Number(true)
let falseNum=Number(false)
 
 
console.log(trueNum);  // 1
console.log(falseNum); // 0
 
console.log(typeof(trueNum))     //number
console.log(typeof(falseNum))    //number
                            
                        
                            
                        

Convert boolean to string

                            
let trueStr=String(true);
let falseStr=String(false);
 
console.log(trueStr)      //true
console.log(falseStr)     //false
 
console.log(typeof(trueStr))      //string
console.log(typeof(falseStr))     //string
                            
                        
                            
                        

Summary

The boolean is a primitive type in JavaScript. The JavaScript also has the Boolean object. Always use the primitive boolean. When converting the other data types to boolean remember that zero, empty string, null, undefined, NaN returns false. Everything else returns true.

Read More

  1. JavaScript Tutorial
  2. Data Types in JavaScript
  3. JavaScript String
  4. Template Strings & String Interpolation
  5. Tagged Templates
  6. String to Number
  7. EPSILON & Floating Point Precision
  8. Infinity
  9. BigInt
  10. BintInt Vs Number
  11. Undefined