Javascript Boolean Type check if a boolean variable is initialized

Introduction

Using the typeof operator, we can check the datatype of a variable.

If the variable is a Boolean value, typeof will return the string 'boolean'.

var a = Boolean(true); 
var b = false; //w  w  w  . j a v  a2s.  com
var c = ""; 
var d = new Date(); 

if ( typeof a === 'boolean' ) {
   console.log("a is a Boolean"); 
} else { 
   console.log("a is not a Boolean"); 
} 

if ( typeof b === 'boolean' ) {
   console.log("b is a Boolean"); 
} else { 
   console.log("b is not a Boolean"); 
} 

if ( typeof c === 'boolean' ) {
   console.log("c is a Boolean"); 
} else { 
   console.log("c is not a Boolean"); 
} 

if ( typeof d === 'boolean' ) {
    console.log("d is a Boolean"); 
} else { 
    console.log("d is not a Boolean"); 
} 



PreviousNext

Related