JavaScript Data Types: Booleans

By: Dusty Arlia
Published on August 9, 2012
Last Updated on Saturday, July 11, 2015 at 6:51 PM
Total Updates: 7

With the Boolean data type, there are only two values: true and false. JavaScript uses the keywords true and false to represent those values. You will use the Boolean data type when you need the answer to a yes or no (true and false respectively) question. That most often occurs in conditional statements (if/else statements), loops (for loops and while loops), and in flags to test whether something is true of false.

When initializing a variable with a Boolean value, you must leave the quotes off. If you were to use quotes, you would be setting your variable to a string value. For example:

var boolean_variable = true;
var string_variable = "true";
alert("boolean_variable has a data type of " + typeof (boolean_variable));
alert("string_variable has a data type of " + typeof (string_variable));
This example prints truetrue to the screen and then an alert box with the text boolean_variable has a data type of boolean pops up, followed by another alert box that has the text string_variable has a data type of string.