# Stop Using Boolean() in JavaScript

## Use the double negation technique instead

Feb 27 · 2 min read

Boolean values—named after English mathematician George Boole—are either true or false.

You’ll see the term boolean appear in branches of math such as boolean algebra. In programming, we rely on boolean values for determining the flow of our control structures, such as `if` statements and `while` loops.

When we need to convert a value to either `true` or `false`, called boolean coercion, JavaScript has the `Boolean()` built-in function.

If you need a quick, one-time, boolean coercion, there’s a better way.

# The Logical Negation Operator

First, we need to introduce the logical negation operator, also known as an exclamation point. The operator’s name is derived from Boolean Algebra’s negation gate and it effectively flips a value from true to false and vice versa.

`console.log(!true);  // falseconsole.log(!false); // truelet x = 5;console.log(x != 3); // true`

But what happens if `!` does not precede a boolean value?

If a value that is not boolean follows the operator, the value will be coerced into a boolean value, then negated.

`console.log(!0);    // trueconsole.log(!5);    // falseconsole.log(!"");   // trueconsole.log(!null); // true`

This gets into a conversation about truthy and falsy values. The tl;dr is that certain values such as empty strings, the integer zero, and null will evaluate to false during boolean coercion. Hence, they are falsy (false-ish) values.

# Double Negation Operator?

Now that we know the logical negation operator has the ability to perform boolean coercion, all we need to do is re-negate the boolean value to preserve its original coerced value.

`console.log(!!5);    // trueconsole.log(!!"");   // falseconsole.log(!!null); // false`

Is `!!` an operator? No. Technically it’s just a double negation operator. But that’s OK because we get a functional replacement for `Boolean()` with only two characters.

Now, I know that some people will kick and scream about `!!` being hard to read — I can understand that. But now you have options.

Do you think the double negation technique is easier to read or do you prefer using `Boolean()`? Share your thoughts and comments below. Thanks for reading!

Written by

## More From Medium

### Should You Learn VIM as a Developer in 2020?

Mar 19 · 4 min read

### Fun Side Projects That You Can Build Today

Mar 11 · 6 min read

### The Zero-Dollar Infrastructure Stack

Mar 20 · 5 min read

#### 1.1K

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just \$5/month. Upgrade