Home

The Boolean Data Type in JavaScript

|
Updated:  
2016-03-26 07:26:28
|
JavaScript Essentials For Dummies
Explore Book
Buy On Amazon

The Boolean data type in JavaScript can store one of two possible values: true or false. Boolean values are the result when you do comparisons in JavaScript. If you ask JavaScript something like: "Is 3 equal to 30?," it will respond with a Boolean value of false.

The Boolean data type is named after the mathematician George Boole, so it's always capitalized.

Do a few experiments with Booleans. Open the JavaScript Console and try typing each of the following statements, pressing Return or Enter after each one to see the result. Note that a single‐line comment is used after each statement to explain what it means. You don't need to type these comments into the console, but you can if you want.

1 < 10 // Is 1 less than 10?
100 > 2000 // Is 100 greater than 2000?
2 === 2 // Is 2 exactly equal to 2?
false === false // Is false exactly equal to false?
40 >= 40 // Is 40 greater than or equal to 40?
Boolean (0) // What is the Boolean value of 0?
Boolean (false) // What is the Boolean value of false?
"apples" === "oranges" // Is "apples" exactly equal to "oranges"?
"apples" === "apples" // Is "apples" exactly equal to "apples"?

In addition to the statements you would expect to be false, JavaScript also considers the following values to be false:

0 null undefined
" (an empty string) false

About This Article

This article is from the book: 

About the book author:

Chris Minnick is an accomplished author, teacher, and programmer. Minnick authored or co-authored over 20 books, including titles in the For Dummies series. He has developed video courses for top online training platforms and he teaches programming and machine learning to professional developers at some of the largest global companies.

Eva Holland is an experienced web developer, tech trainer, and coauthor of Coding with JavaScript For Dummies. She is a co-founder of WatzThis?, a company focused on training and course development.