The "Boolean" Object Type - Wrapping Boolean Values into Objects

This section provides a quick description and a tutorial example script on the 'Boolean' built-in object type, which is used to create 'Boolean' objects.

The "Boolean" object type is a special built-in object type to be used to create a "Boolean" object, which is different than a Boolean value of the Boolean primitive type. It has the following main features:

Here is a tutorial example script showing some of those features:

// Boolean_Object_Type.js
// Copyright (c) 2013 HerongYang.com. All Rights Reserved.

   // Creating a new Boolean object
   var isBetter = new Boolean("JavaScript">"Java");

   // Checking this object
   println("\nAbout this \"isBetter\":");
   println("   Type = "+(typeof isBetter));
   println("   Instance Of Object: "+(isBetter instanceof Object));
   println("   Instance Of Boolean: "+(isBetter instanceof Boolean));

   // Using Boolean's inherited methods
   var v = isBetter.valueOf();
   var s = isBetter.toString();

   println("\nExecution result:");
   println("   typeof isBetter = "+(typeof isBetter));
   println("   typeof isBetter.valueOf() = "+(typeof v));
   println("   typeof isBetter.toString() = "+(typeof s));

   println("\nConverting to Boolean objects on the fly:");
   println("   \"...\".toString() = "
      +(("JavaScript">"Java").toString()));

If you run this script with "jrunscript", you will get:

About this "isBetter":
   Type = object
   Instance Of Object: true
   Instance Of Boolean: true

Execution result:
   typeof isBetter = object
   typeof isBetter.valueOf() = boolean
   typeof isBetter.toString() = string

Converting to Boolean objects on the fly:
   "...".toString() = true

Now we know how to create "Boolean" objects with the "Boolean()" constructor.

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

Introduction to Built-in Object Types

 Overview of Built-in Object Types

 The "Object" Object Type - The Root Object Type

 The "Global" Object Type - The Invisible Global Container

 Global Properties and Functions Defined in ECMAScript

 Global Properties and Functions Provided by "jrunscript"

 The "Function" Object Type - Functions Are Objects

 The "Array" Object Type - Arrays Are Objects

 The "JSON" Object Type - parse() and stringify()

 The "String" Object Type - Not Equal to String Primitive Type

The "Boolean" Object Type - Wrapping Boolean Values into Objects

 The "Number" Object Type - Not Equal to Number Primitive Type

 The "Date" Object Type - Managing Dates and Times

 The "RegExp" Object Type - Regular Expression Patterns

 The "Error" Object Type - Runtime Exceptions

 The "Math" Object Type - The Math Container

 W3C's Document Object Model (DOM) Specifications

 AJAX (Asynchronous JavaScript And XML)

 References

 Full Version in PDF/EPUB