How to Check for a Boolean in JavaScript

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.

Image for postPhoto 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 ? 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

  • Patrick Divine breaks down double bangs (!!) on his Medium blog:

Javascript ?Bang, Bang. I Shot You down? – Use of double bangs (!!) in Javascript.

How to keep from getting shot down while using double bangs

  • Marius Schulz explains Javascript?s && (AND) and || (OR) on his blog:

The && and || Operators in JavaScript

Similar to other C-like programming languages, JavaScript defines the two operators && and || which represent the?

  • Brandon Morelli explains &&, ||, and !(NOT) in

Learn JavaScript: Logical AND / OR / NOT

Learn and Understand the JavaScript Logical Operators: AND (&&), OR (||), and NOT (!)

  • Chris Love discusses the ?sorcery? that is the !! operator on his blog:

What is the !! (not not) operator in JavaScript?

The other day I was rolling through some JavaScript to figure out how a 3rd party library ticked. As I scanned the?

  • The site has an overview of JavaScript booleans:

JavaScript Booleans Explained ? How to use Booleans in JavaScript

Boolean Boolean is a primitive data type commonly used in computer programming languages. By definition, a boolean has?

  • JavaScript Tutorial on why Boolean() is good, but new Boolean() is bad:

JavaScript Boolean vs. boolean: Explained By Examples

Summary: in this tutorial, you will learn about the JavaScript Boolean object and the differences between the Boolean?

Image for postPhoto by Wouter Meijering on Unsplash


No Responses

Write a response