Tutorials Tonight's Logo
Follow Us


The boolean in javascript is a primitive data type that has two values true representing yes and false representing no.

boolean is generally used in conditional statements or at place when we have to control the flow example loops, if-else, switch etc.

var positive = true; // boolean
var negative = false; // boolean
var x = "true"; // String
var y = "false"; // String

console.log(typeof positive);
console.log(typeof negative);
console.log(typeof x);
console.log(typeof y);
▶ Run the code

The Boolean Constructor

Boolean() is a function which can be used to create boolean values.

The Boolean() function can receive variables or expressions as arguments.

var positive = Boolean(true);
var negative = Boolean(false);
var x = Boolean(10 > 20);
var y = Boolean(15 === 15);
var z = Boolean(20 < 30);

console.log(typeof positive);
console.log(typeof negative);
console.log(x, y, z);
▶ Run the code

Note: A variable or any object which has some value is considered as true in javascript and one which has no value is considered as false example 0, undefined, null, "" etc.

Lets see an example.

console.log(Boolean(1 < 2));
console.log(Boolean("Some string"));
▶ Run the code

Boolean as Object

Generally Boolean is a primitive data type when created using literal. Example var bool = true;.

But booleans can be made objects using new keywords. Example var boolObj = new Boolean(true);.

var bool = true; // boolean
var boolObj = new Boolean(true); // object
console.log(typeof bool);
console.log(typeof boolObj);
▶ Run the code

Note: Use literals to define boolean because boolean as object is slow.

Boolean objects are not equal to boolean literals and also boolean objects are slower.

var bool = true; // boolean
var boolObj = new Boolean(true); // object
var boolObj2 = new Boolean(true); // object
console.log(bool == boolObj);
console.log(bool === boolObj);
console.log(boolObj == boolObj2);
▶ Run the code

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.