JavaScript Boolean prototype attribute

Definition and usage

prototype Is the global constructor function available for all JavaScript objects.

Boolean.prototype refers to the global Boolean() object.

prototype The constructor allows you to add new properties and methods to boolean values.

When a new property is constructed, all arrays will obtain this property and its value.

When a new method is constructed, all arrays will obtain this method.

Instance

Create a new method for JavaScript boolean values:

Boolean.prototype.myColor = function() {
  if (this.valueOf() == true) {
    return "green";
  } else {
    return = "red";
  }
;

Create a boolean value and then call myColor():

let a = true;
a.myColor()    // Returns green

Try it yourself

Syntax

Boolean.prototype.name = value

Browser support

All browsers fully support Boolean.prototype:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related Pages

Tutorial:JavaScript Boolean

Tutorial:JavaScript Data Types

Tutorial:JavaScript Object Constructor