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
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
Everything with a Real Value is True.
- “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.