JavaScript Boolean

JavaScript Boolean  is section 8 of Beginner’s Guide to JavaScript.

Very often, in programming, you will need a data type that can only have one of two values. We called it as binary values.

  • YES / NO
  • ON / OFF
  • TRUE / FALSE
  • 0 / 1

A JavaScript Boolean represents one of two values: true or false.

The Boolean() Function

You can use the Boolean() function to find out if an expression (or a variable) is true.

Boolean(20 > 19)        // returns true

(20 > 19)              // also returns true
20 > 19                // also returns true

 

Boolean is mostly related with comparison operators and conditional statements. The Boolean value of an expression is the fundamental for JavaScript comparisons and conditions.

Everything with a Real Value is True. 

  • 100
  • 3.14
  • -15
  • “Hello”    ———–    Any (not empty) string is true
  • “false”   ———–    Even the string ‘false’ is true
  • 7 + 1 + 3.14 Any expression is (exept zero) is true

Everything Without a Real Value is False

  • The Boolean value of 0 (zero) is false. 
  • The Boolean value of -0 (minus zero) is false. 
  • The Boolean value of “” (empty string) is false.
  • The Boolean value of undefined is false.
  • The Boolean value of null is false.
  • The Boolean value of false is (you guessed it) false.
  • The Boolean value of NaN is false.

 


What’s Next?

Learn what includes in JavaScript Math

 

 

Written by Sandeep Likhar

Sandeep Likhar is from India, where he is a blogger, eBook designer, and founder of LetsDnD. He has 6 years of experience in the industry as a Digital Publishing Expert and eBook Converter, providing services to authors, publishers, and distributors worldwide. He is proficient in converting books into various formats, such as HTML, epub, mobi, word, PDF, including all major online platforms like iTunes, Kobo, Kindle, CreateSpace, B&N, Smashwords, and more.

Comment Below