Search⌘ K
AI Features

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.

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:

Node.js
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:

Node.js
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: