Boolean in JavaScript: true, false, and what turns into them

The Boolean type holds only two values, true and false, and it carries every single decision a JavaScript program ends up making.
3 min read
Believemy logo

It is the simplest type in the language: two values, nothing else. It nevertheless sits at the center of everything, since every condition, every filter and every test eventually reduces to a boolean.

The difficulty never comes from the type itself, but from what JavaScript accepts in its place. An if does not demand a boolean, it converts whatever it is given, and that is where the surprises start.


Definition

The Boolean type admits only two values: true and false. They are written without quotes, in lowercase, and must never be confused with the strings "true" and "false", which are text.

JAVASCRIPT
const isPaid = true;

console.log(typeof isPaid);       // "boolean"
console.log(49 > 20);             // true, a comparison produces a boolean
console.log(!isPaid);             // false, negation flips it

console.log(Boolean("false"));    // true: it is a non-empty string

Comparison operators, Strict equality (===), the in operator and instanceof all return a boolean. It is the output format of every question asked of a program.


Converting to a boolean

Two spellings exist and give exactly the same result.

  • Boolean(value) converts explicitly. It is the more readable form, particularly for someone starting out.
  • !!value applies negation twice, which amounts to the same thing. Very widespread, shorter, but opaque at first glance.

The conversion rule is described in detail in Truthy and falsy: only eight values become false, everything else becomes true.

JAVASCRIPT
const values = [1, 0, "text", "", null, 2];

console.log(values.filter(Boolean));  // [1, "text", 2]

Passing Boolean straight to Array.filter() is the standard idiom for stripping every empty value out of an Array in one expression.


What looks like a boolean without being one

The logical operators && and || do not return a boolean but one of their two operands.

JAVASCRIPT
console.log(true && "access granted");  // "access granted"
console.log(false || "default value");  // "default value"

That is what allows them to pick a fallback value. But as soon as 0 or an empty string belong to the legitimate values, prefer Nullish coalescing (??), or Ternary operator (?:) when both branches are explicit.

Good to know

Never write new Boolean(false). That wrapper object is always truthy, including when it holds false, and serves no purpose today.


Frequently asked questions

Question

Should I convert before an if?

It is not necessary, the condition already does it. Explicit conversion earns its place elsewhere: when a value has to be stored, sent to an API or compared strictly. Writing Boolean(x) inside an if adds noise without changing the behavior.


Question

How should I name a boolean variable?

The convention is to start with a state verb: isActive, hasPaid, canEdit. A name that reads like a question makes the condition understandable without going back to the declaration. Avoid negations in the name, which produce unreadable double negatives.


Question

How do I test that at least one element matches?

The Array.some() method returns a boolean directly: it stops at the first matching element. Its twin every answers the opposite question, whether all elements match. Both avoid writing a loop with a flag, which is longer and easier to break.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.