JavaScript Boolean Object

In this chapter, we will learn about the JavaScript Boolean object. The Boolean object is a wrapper object that allows you to work with boolean values. We will cover:

  • What is the Boolean Object?
  • Creating Boolean Objects
  • Boolean Properties
  • Boolean Methods
  • Working with Boolean Values
  • Simple Programs using Boolean Object

What is the Boolean Object?

The Boolean object in JavaScript is a wrapper object that allows you to work with boolean values (true or false). It provides several properties and methods to manipulate and interact with boolean values.

Creating Boolean Objects

You can create a Boolean object using the Boolean() constructor or by directly using boolean values.

Syntax

let bool = new Boolean(value);
let bool = Boolean(value);
let bool = value;

Example

let bool1 = new Boolean(true);
let bool2 = Boolean(false);
let bool3 = true;

console.log(bool1); // [Boolean: true]
console.log(bool2); // false
console.log(bool3); // true

Boolean Properties

The Boolean object has a single property, constructor, which returns the function that created an instance of the Boolean object.

Example

let bool = new Boolean(true);
console.log(bool.constructor);

Output:

[Function: Boolean]

Boolean Methods

The Boolean object provides several methods to perform operations on boolean values.

toString()

Converts a boolean value to a string.

let bool = true;
console.log(bool.toString());

Output:

"true"

valueOf()

Returns the primitive value of a Boolean object.

let bool = new Boolean(false);
console.log(bool.valueOf());

Output:

false

Working with Boolean Values

JavaScript automatically converts values to boolean when needed, such as in conditional statements.

Example

let x = 10;
if (x) {
  console.log("x is true");
} else {
  console.log("x is false");
}

Output:

x is true

Simple Programs using Boolean Object

Program 1: Converting Values to Boolean

let values = [0, 1, "", "Hello", null, undefined, [], {}];

values.forEach(value => {
  let bool = Boolean(value);
  console.log(`Value: ${value}, Boolean: ${bool}`);
});

Output:

Value: 0, Boolean: false
Value: 1, Boolean: true
Value: , Boolean: false
Value: Hello, Boolean: true
Value: null, Boolean: false
Value: undefined, Boolean: false
Value: , Boolean: true
Value: [object Object], Boolean: true

Program 2: Checking Truthy and Falsy Values

function checkTruthyFalsy(value) {
  let bool = Boolean(value);
  if (bool) {
    console.log(`Value: ${value} is truthy`);
  } else {
    console.log(`Value: ${value} is falsy`);
  }
}

checkTruthyFalsy(0);
checkTruthyFalsy(1);
checkTruthyFalsy("");
checkTruthyFalsy("Hello");
checkTruthyFalsy(null);
checkTruthyFalsy(undefined);
checkTruthyFalsy([]);
checkTruthyFalsy({});

Output:

Value: 0 is falsy
Value: 1 is truthy
Value:  is falsy
Value: Hello is truthy
Value: null is falsy
Value: undefined is falsy
Value:  is truthy
Value: [object Object] is truthy

Conclusion

In this chapter, you learned about the JavaScript Boolean object, including how to create Boolean objects, boolean properties, boolean methods, and working with boolean values. We also provided various use cases with simple programs to demonstrate the usage of the Boolean object. Understanding how to effectively use the Boolean object is essential for performing logical operations in JavaScript.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top