Working With the Boolean Type
In this lesson, we shall learn about the Boolean type and how to use it in JavaScript. Let's begin!
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:
var isItValid = true;var isTheGlobeFlat = false;var flag = 3 < 4;console.log(isItValid);console.log(isTheGlobeFlat);console.log(flag);
With the help of the Boolean()
casting function, you can convert any value to a Boolean:
var thisValue = Boolean("false");console.log(thisValue);
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: