JavaScript boolean.prototype Property

Shubham Vora Jan 30, 2023
  1. Syntax of JavaScript boolean.prototype Property
  2. Example 1: Use boolean.prototype to Get the Value Added With Different Object
  3. Example 2: Use boolean.prototype With the Function to Create a Prototype for the Object
JavaScript boolean.prototype Property

In JavaScript, the .prototype property is available with all objects. This method allows us to add new properties to objects.

Syntax of JavaScript boolean.prototype Property

boolean.prototype.func = function(){
    if(this.valueOf()){
	/* perform some operation */
	}
}
let val = false;
val.func();

Parameters

val It is a Boolean value on which we need to call any function defined in boolean.prototype.
this Users can access the value of the val variable using the this keyword.

Return

This method returns the value or function used to create the prototype.

Example 1: Use boolean.prototype to Get the Value Added With Different Object

In JavaScript, the boolean.prototype property allows us to add custom value or function as a prototype for different objects.

In this example, we have created an empty object and added a custom value that will be added to the output by using the boolean.prototype property.

function Obj(){}
const object = new Obj();
Obj.prototype.value = "Add word";
console.log(object.value);

Output:

Add word

Example 2: Use boolean.prototype With the Function to Create a Prototype for the Object

In JavaScript, the boolean.prototype property uses a function to create the prototype for an object. In this example, we have created an object and a function.

Then, we used the boolean.prototype property to set a prototype for the object.

Boolean.prototype.setvalue = function() {
if (this.valueOf() == false){
   return "add value";
} else {
   return " function";
   }
};
let object = false;
console.log(object.setvalue());

Output:

add value

The boolean.prototype method is supported in all browsers.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript Boolean