avatarDr. Derek Austin 🥳

Summary

The web content provides a comprehensive guide on checking for and producing Boolean values in JavaScript, along with an explanation of truthy and falsy values and the historical origin of the term "Boolean."

Abstract

The article titled "How to Check for a Boolean in JavaScript" explains that Booleans in JavaScript are primitives with only two possible values: true or false. It details how to use the typeof operator to verify a Boolean, demonstrates the use of reserved keywords true and false, and explains the concept of truthy and falsy values. The article also covers how to coerce expressions into Booleans using the Boolean() function or double negation !!. It clarifies that only true and false are of type "boolean" and provides examples to illustrate these concepts. Additionally, the article introduces the origins of Boolean algebra, attributing the term "Boolean" to English mathematician George Boole, and concludes with a list of further reading resources for readers to deepen their understanding.

Opinions

  • The article suggests that understanding Booleans and their truthy or falsy nature is essential for writing effective JavaScript code.
  • It emphasizes the importance of distinguishing between actual Boolean primitives and values that are merely truthy or falsy when type checking.
  • The use of Boolean() or !! is presented as a straightforward way to cast any value into a Boolean primitive, highlighting the practicality of these methods.
  • The inclusion of external resources, such as MDN Docs and articles from JavaScript experts, indicates a collaborative approach to learning and reinforces the article's credibility.
  • The historical context of George Boole's contribution to mathematical logic is included to enrich the reader's understanding of the significance of Booleans in programming.

How to Check for a Boolean in JavaScript

Boolean values, true or false, are one of the easiest primitives to check for in JavaScript — just use the typeof operator.

Photo by Wouter Meijering on Unsplash

Booleans are true or false

Checking for boolean values is easy in JavaScript— they are going to be either true or false, and typeof a boolean is always "boolean".

“Boolean type — Boolean represents a logical entity and can have two values: true and false. See Boolean and Boolean for more details.” — MDN Docs

Generally, we use the keywords true and false to indicate booleans, though sometimes we rely on truthy and falsy values, which I explain later.

The following code shows how to check for boolean values in JavaScript:

How to produce Boolean values in JavaScript

To access the primitive data type, boolean, we usually use the keywords true and false, which are reserved for booleans.

However, we can also evaluate other variables as meaning true or false.

“In addition to the boolean primitive type, JavaScript also provides you with the global Boolean() function, with the letter B in uppercase, to cast a value of another type to boolean." — JavaScript Tutorial

We can coerce any expression in JavaScript to a boolean in one of 3 ways:

  1. Using the Boolean() wrapper function.
  2. Putting two exclamation points >!! (the logical NOT ! operator, twice) in front of any expression, which calls the Boolean() wrapper function.
  3. Putting any expression inside of a conditional, such as an >if statement, question mark e>? operator, or loop — with or without AND >&& or OR >||.

Here are examples of coercing expressions to booleans in JavaScript:

Truthy and falsy booleans in JavaScript

In conditionals, values that evaluate to false are called falsy values, and everything else in JavaScript are truthy values that evaluate to true.

The falsy values in JavaScript are false, 0, >-0, 0n, null, undefined, NaN, and the empty string >""; everything else is a truthy value.

All objects are truthy, including empty objects {} and arrays [].

The following code gives some examples of truthy and falsy values:

Conclusion: Type checking for JavaScript booleans

If you need to be sure you have a boolean primitive value, and not just a falsy value, check the type of the JavaScript variable using typeof.

Only true and false have a typeofequal to "boolean".

You can coerce any JavaScript expression to a boolean primitive type using the Boolean() wrapper function, which is available on the global object.

The Boolean() wrapper can also be invoked using !! (Bang! Bang!).

And, that’s really all there is to know to check for a Boolean! 😊😂❤️😍👌

Do you know where the term Boolean comes from?

The Boolean value is named after English mathematician George Boole, who pioneered the field of mathematical logic. — MDN Docs

Further Reading

Photo by Wouter Meijering on Unsplash

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

JavaScript
Programming
Software Development
Data Science
Software Engineering
Recommended from ReadMedium