The Boolean() constructor is used to manufacture new instances of the built-in Boolean object.
When the Boolean() constructor is called by the new operator, it initializes a brand new Boolean object instance.
The value of the new Boolean object instance is the same as the Boolean value yielded by the type conversion of the Boolean() constructor's parameter.
Value: | Result: |
---|---|
No value | Always false |
undefined | Always false |
null | Always false |
Boolean | No conversion, the input value is returned unchanged |
Number | The result is false if the argument is 0 or NaN, otherwise it is true |
String | Zero length strings return false otherwise the result is true |
Object | Always true |
The result of calling the constructor is a Boolean object whose value is true or false depending on the input value. If the value-input parameter is omitted, then a Boolean object with value false is returned by default.
Note that unlike the Object() constructor, which can be called without its parentheses, calling the Boolean() constructor without parentheses yields an uninitialized object.
Note also that using Boolean objects in conditional code is prone to risks due to the fact that all objects yield a Boolean true value when tested in logical expressions. This includes Boolean objects whose present value is false.
ECMA 262 edition 2 - section - 15.1.3.5
ECMA 262 edition 2 - section - 15.6.1
ECMA 262 edition 2 - section - 15.6.2
ECMA 262 edition 2 - section - 15.6.3
ECMA 262 edition 2 - section - 15.6.3.1
ECMA 262 edition 3 - section - 15.6.2
Prev | Home | Next |
Boolean object | Up | Boolean() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |