Working With the Boolean Type
Explore the Boolean type in JavaScript, including how to declare Boolean variables and convert other types to Boolean values. Understand key conversion rules such as which values are true or false, and see examples to clarify behavior. This lesson helps you grasp Boolean logic essential for controlling program flow and prepares you for working with other JavaScript types.
We'll cover the following...
Boolean values are the basis of most flow-control statements because they represent either true or false; as you remember, these are Boolean literals and reserved words, too.
You can declare Boolean variables with these literals, or expressions resulting in a Boolean value:
With the help of the Boolean() casting function, you can convert any value to a Boolean:
You must be careful with Boolean conversion because it follows different rules than you may think.
For example, the thisValue variable will hold true after this conversion, instead of false suggested by the right side of the assignment statement.
To understand the above phenomenon, here are the conversion rules:
Some handy Boolean conversion rules
Boolean literals are converted to their appropriate Boolean values according to the following rules: