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() > Certain Things Are false, Everything Else Is true

Certain Things Are false, Everything Else Is true

It has already been mentioned, but is worth mentioning again because it pertains to conversions. If a value is 0, −0, null, false, NaN, undefined, or an empty string(""), it is false. Any value in JavaScript except the aforementioned values will be converted to true if used in a boolean context (i.e., if (true) {};).

Live Code

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

// all of these return a false boolean value
console.log(Boolean(0));
console.log(Boolean(-0));
console.log(Boolean(null));
console.log(Boolean(false));
console.log(Boolean(''));
console.log(Boolean(undefined));
console.log(Boolean(null));

// all of these return a true boolean value
console.log(Boolean(1789));
console.log(Boolean('false')); /* 'false' as a string is not false 
                               the boolean value */
console.log(Boolean(Math));
console.log(Boolean(Array());

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

  

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