Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Boolean() > Non-Primitive False Boolean Objects Convert to true

Non-Primitive False Boolean Objects Convert to true

A false boolean object (as opposed to a primitive value) created from the Boolean() constructor is an object, and objects convert to true. Thus, when creating a false boolean object via the Boolean() constructor, the value itself converts to true. Below, I demonstrate how a false boolean object is always “truthy.”

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var falseValue = new Boolean(false);

console.log(falseValue); // we have a false boolean object, but objects are truthy

if (falseValue) { // boolean objects, even false boolean objects, are truthy
   console.log('falseValue is truthy');
}

</script></body></html>

If you need to convert a non-boolean value into a boolean, just use the Boolean() constructor without the new keyword and the value returned will be a primitive value instead of a boolean object.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint