Thursday, September 12, 2013

Non-primitive [false] Boolean object is actually [true]

A Boolean object with false parameter (not the primitive false value) which generated from the Boolean() constructor, the object value will be true to check you may use it in an "if" condition.

If a value is false, undefined, null, NaN, 0, -0, or an empty string(""), it is false! but any other value in JavaScript will be converted to true if used in a Boolean context.

<!DOCTYPE html> <html lang="en"> <body> <script> var myBoolean = new Boolean(false); console.log( myBoolean ); console.log( myBoolean.toString() ); console.log( myBoolean.valueOf() ); if( myBoolean ) console.log( typeof myBoolean ); </script> </body> </html>

Share/Bookmark

No comments: