Team LiB
Previous Section Next Section

Boolean

Boolean is the built-in object corresponding to the primitive Boolean data type. This object is extremely simple. It has no interesting properties of its own. It inherits all of its properties and methods from the generic Object. So it has toSource(), toString(), and valueOf(). Out of these, maybe the only method of practical use is the toString() method, which returns the string “true” if the value is true or “false” otherwise. The constructor takes an optional Boolean value indicating its initial value:

var boolData = new Boolean(true);

However if you don’t set a value with the constructor, it will be false by default.

var anotherBool = new Boolean();
// set to false

Because of some subtleties in JavaScript’s type conversion rules, it is almost always preferable to use primitive Boolean values rather than Boolean objects.


Team LiB
Previous Section Next Section