# Boolean : Object
Return to: JavaScript Core index

The Boolean object is an object wrapper for a boolean value.

## Platform Support

IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | ---
3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+

## Constructors

Constructor | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
Boolean Constructor(Boolean value) : Boolean Creates a new instance of a Boolean object. | Show Details | 3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+

### Boolean(Boolean value) : Boolean

Creates a new instance of a Boolean object.

Parameters

Boolean | value | The initial value of the Boolean object. The value is converted to a boolean value, if necessary. If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.

Returns

Boolean |

## Properties

Property | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
constructor : Object Specifies the function that creates the Boolean prototype. | Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+

See Also

  Object.constructor

Availability

  JavaScript 1.1|JScript 2.0|ECMAScript v1

prototype : Object Represents the Boolean prototype object. | Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+

See Also

  Function.prototype

Availability

  JavaScript 1.1|JScript 2.0|ECMAScript v1

## Functions

Method | Action | IE | Mozilla | Netscape | Opera | Safari
--- | --- | --- | --- | --- | --- | ---
toSource() : String Returns a string representing the source code of a Boolean object. | Show Details | 4.0+ | 1.0+ | 4.0+ | no | no

Returns

String |

Remarks

  The `toSource` method returns the following values:

  - For the built-in `Boolean` object, `toSource` returns the following string indicating that the source code is not available:

      function Boolean() {
                              [native code]
                              }

  - For instances of `Boolean`, `toSource` returns a string representing the source code.

  This method is usually called internally by JavaScript and not explicitly in code.

See Also

  Object.toSource

Availability

  JavaScript 1.3

toString() : String Returns a string representing the specified Boolean object. | Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+

Returns

String |

Remarks

  The Boolean object overrides the `toString` method of the Object object; it does not inherit Object.toString. For Boolean objects, the `toString` method returns a string representation of the object.

  JavaScript calls the `toString` method automatically when a Boolean is to be represented as a text value or when a Boolean is referred to in a string concatenation.

  For Boolean objects and values, the built-in `toString` method returns the string "`true`" or "`false`" depending on the value of the boolean object. In the following code, `flag.toString` returns "`true`".

      var flag = new Boolean(true)
                              var myVar=flag.toString()

Availability

  JavaScript 1.1|JScript 2.0|ECMAScript v1

valueOf() : Boolean Returns the primitive value of a Boolean object. | Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+

Returns

Boolean |

  ### Using `valueOf`

      x = new Boolean();
                              myVar=x.valueOf()      //assigns false to myVar

Remarks

  The `valueOf` method of Boolean returns the primitive value of a Boolean object or literal Boolean as a Boolean data type.

  This method is usually called internally by JavaScript and not explicitly in code.

Throws
    Throws TypeError if the object is not a Boolean.
See Also

  Object.valueOf

Availability

  JavaScript 1.1|JScript 2.0|ECMAScript v1

### Creating `Boolean` objects with an initial value of false

    bNoParam = new Boolean();
             bZero = new Boolean(0);
             bNull = new Boolean(null);
             bEmptyString = new Boolean("");
             bfalse = new Boolean(false);

### Creating `Boolean` objects with an initial value of true

    btrue = new Boolean(true);
             btrueString = new Boolean("true");
             bfalseString = new Boolean("false");
             bSuLin = new Boolean("Su Lin");

## Remarks

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object whose value is not `undefined` or `null`, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:

    x = new Boolean(false);
                if (x) //the condition is true

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to `false`:

    x = false;
                if (x) //the condition is false

Do not use a `Boolean` object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

    x = Boolean(expression);     //preferred
                x = new Boolean(expression); //don't use

If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.

    myFalse = new Boolean(false);   // initial value of false
                g = new Boolean(myFalse);       //initial value of true
                myString = new String("Hello"); // string object
                s = new Boolean(myString);      //initial value of true

Do not use a Boolean object in place of a Boolean primitive.

## References

Object

## Availability

JavaScript 1.1|JScript 2.0|ECMAScript v1

text_javascript aptana_docs