Skip to content

Boolean object in JavaScript | Example code

  • by

JavaScript Boolean object represents two values, either “true” or “false“. To create an object with Boolean and set the Boolean value “true” or “false”, which are the Boolean literals. JavaScript Boolean object is a member of global objects and a wrapper class.

var x = new Boolean(false);
var x = new Boolean(true);

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

var x = new Boolean(false);
if (x) {
  // this code is executed

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

var x = false;
if (x) {
  // this code is not executed

Boolean object in JavaScript

Simple example code.

<!DOCTYPE html>

   var a = new Boolean(false)

   console.log("Object constructed : " + a.constructor);




Boolean object in JavaScript

Boolean Objects Property

constructorSpecifies the function that creates an object’s prototype.Implemented in JavaScript 1.1
prototypeUse to add new properties and methods to a boolean object.Implemented in JavaScript 1.1

Boolean Objects Methods

toSourceReturns a string which represents the source code of a boolean object.Implemented in JavaScript 1.1
toStringReturns a string representing the specified boolean object.Implemented in JavaScript 1.1
valueofReturns the primitive value of a boolean object.Implemented in JavaScript 1.1

Boolean Object False

JavaScript Boolean Object will have the initial value as false if the value provided at the time of object creation is 0, -0, NaN, null, undefined, false, empty string, or even if no value is provided because the default value is also false.

let obj1 = new Boolean();
let obj2 = new Boolean(0);
let obj3 = new Boolean(null);
let obj4 = new Boolean('');
let obj5 = new Boolean(false);

Boolean Object True

Apart from the values specified above, for which the initial value of the Boolean object is false, all other values will set the value as true. Let’s take a few examples,

let obj1 = new Boolean(true);
let obj2 = new Boolean('true');
let obj3 = new Boolean('false');
let obj4 = new Boolean('hello');
let obj5 = new Boolean([]);
let obj6 = new Boolean({});

Comment if you have any doubts or suggestions on this JS Object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading