JavaScript -Boolean

By Published 2 min read

In JavaScript, the Boolean data type plays a crucial role in decision-making and controlling the flow of your code. Booleans are all about representing two fundamental states: true and false. They’re named after the mathematician George Boole, who developed a system of algebra that deals with logical values and operations.

Here’s a breakdown of Booleans in JavaScript:

Creating Boolean Values:

You can create Boolean variables by simply assigning them the values true or false. For instance:

let isSunny = true;
let isRainy = false;
Code language: JavaScript (javascript)

These variables can represent conditions or states in your code. For example, isSunny might indicate whether it’s a sunny day, and isRainy might tell you if it’s raining.

Boolean Properties

Let’s explore some essential Boolean properties in JavaScript:

1. constructor Property:

  • The constructor property indicates the constructor function that created a Boolean object. For primitive Boolean values, it refers to the built-in Boolean constructor.
  • Example:
   let boolValue = true;
   console.log(boolValue.constructor); // Output: [Function: Boolean]Code language: JavaScript (javascript)

2. prototype Property:

  • The prototype property is used to add properties and methods to Boolean objects. It allows you to extend the behavior of Boolean objects.
  • Example:
   Boolean.prototype.isTruthy = function() {
       return this.valueOf() === true;
   };

   let myBool = true;
   console.log(myBool.isTruthy()); // Output: trueCode language: JavaScript (javascript)

3. length Property:

  • The length property indicates the number of arguments expected by certain Boolean methods, like toSource().
  • Example:
   console.log(Boolean.prototype.toSource.length); // Output: 0Code language: JavaScript (javascript)

4. name Property:

  • The name property returns the name of the Boolean object.
  • Example:
   console.log(Boolean.name); // Output: "Boolean"Code language: JavaScript (javascript)

5. valueOf() Method:

  • The valueOf() method returns the primitive value of a Boolean object. It is often called implicitly when you use a Boolean object in a context where a primitive value is expected.
  • Example:
   let boolObject = new Boolean(true);
   let primitiveValue = boolObject.valueOf();
   console.log(primitiveValue); // Output: trueCode language: JavaScript (javascript)

6. toString() Method:

  • The toString() method converts a Boolean value to a string representation. You can specify a radix (base) as an argument to convert to binary, octal, or hexadecimal.
  • Example:
   let boolValue = true;
   let binaryString = boolValue.toString(2); // Convert to binary
   console.log(binaryString); // Output: "1"Code language: JavaScript (javascript)

These Boolean properties and methods provide developers with the tools to work with Boolean values effectively, enabling them to perform operations, conversions, and manipulations as needed in JavaScript programming. Understanding these properties can be helpful when working with Boolean objects or when extending their functionality.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume