Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 156 ยท Data Types

Booleans and Truthiness

Learn JavaScript booleans, the eight falsy values, how truthiness works in conditions, and how to convert any value with Boolean() or the !! trick.

A boolean is the simplest type in JavaScript: it has exactly two values, true and false. Yet booleans drive almost every decision your code makes โ€” whether a user is signed in, whether a form is valid, whether a loop should keep going.

JavaScript adds a twist that trips up many beginners: any value can stand in for a boolean. In a condition, a string, a number or an object is treated as either "truthy" or "falsy". Once you know the short list of falsy values, this becomes a handy shortcut instead of a source of bugs. This lesson covers both the real booleans and the rules of truthiness.

true and false

The two boolean values are written as plain keywords, without quotes. typeof reports "boolean" for both:

JS The two boolean values

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Name boolean variables so they read like a yes/no question: isVisible, hasAccess, canEdit, shouldRetry. Then if (isVisible) reads like plain English.

Where booleans come from

You rarely type true or false directly. Most booleans are the result of an expression. Comparison operators such as ===, < and >= always produce a boolean, and so does the ! ("not") operator. Many methods return booleans too:

JS Expressions that produce booleans

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Because these results are ordinary values, you can store them in variables with descriptive names, which often makes conditions much easier to read:

JavaScript
const isAdult = age >= 18;
const hasValidEmail = email.includes("@") && email.includes(".");

if (isAdult && hasValidEmail) {
  // ...
}

You'll study these operators in detail in Comparison and Equality and Logical Operators.

Truthy and falsy values

Conditions in JavaScript โ€” if, while, the ternary ? :, and operators like &&, || and ! โ€” don't require a real boolean. Give them any value and JavaScript decides whether it counts as true or false. A value that counts as true is called truthy; one that counts as false is falsy.

The rule is easy to remember because the falsy list is short. These are all the falsy values in JavaScript:

Value Type
false boolean
0 and -0 number
0n bigint
"" (empty string) string
null null
undefined undefined
NaN number

Everything else is truthy. That includes some values that surprise people:

JS Testing truthiness

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look closely at the surprises:

  • "0", "false" and " " are truthy. They are non-empty strings; JavaScript doesn't look at what the text says.
  • [] (an empty array) and {} (an empty object) are truthy. Every object is truthy, no matter how empty. To check for an empty array, test its length: if (list.length === 0).
  • NaN is falsy, even though it is technically a number.

One historical oddity: in browsers, document.all is an object that is nevertheless falsy. It exists only for compatibility with very old websites, and you will never need it โ€” but it's the single exception to "all objects are truthy".

Converting to a boolean

Sometimes you want an actual true or false rather than "something truthy". There are two common ways to convert.

Boolean(value)

Calling Boolean() as a function (without new) returns true for truthy values and false for falsy ones:

JS Boolean() and the double NOT

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The double NOT: !!value

The ! operator converts its operand to a boolean and flips it. Applying it twice flips it back, leaving a clean boolean. !!value and Boolean(value) always give the same result; choose whichever your team finds clearer. Boolean() is more explicit; !! is shorter and very common in existing code.

The last line of the example shows a neat idiom: filter(Boolean) passes each item to Boolean and keeps the truthy ones, removing empty strings, null and undefined in one step. (Be aware it also removes 0, which may or may not be what you want.)

Never use new Boolean(). With new, you get a boolean wrapper object, and objects are always truthy โ€” so new Boolean(false) behaves like true in an if. Call Boolean() without new.

Truthiness in practice

Truthiness lets you write short, readable checks:

JavaScript
if (username) {
  // username is a non-empty string
}

if (!results.length) {
  // the array is empty (length 0 is falsy)
}

But shortcuts cut both ways. A check like if (count) treats a legitimate 0 the same as a missing value:

JS When truthiness gets it wrong

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first version reports "Unknown stock" for an item that is simply sold out โ€” a bug that's easy to miss in testing, because most test data has non-zero counts. The fixed version only treats undefined and null as unknown.

A good rule of thumb: rely on truthiness when every falsy value really means "nothing here". When 0, "" or false are valid data โ€” a price of zero, an empty comment, a setting that is switched off โ€” compare explicitly. The lessons on null and undefined and nullish coalescing show tools made exactly for this situation.

Booleans as numbers and strings

When a boolean ends up in arithmetic, true becomes 1 and false becomes 0. Converted to a string, they become "true" and "false":

JavaScript
true + true;      // 2
String(false);    // "false"
`Done: ${true}`;  // "Done: true"

Adding booleans can be a quick way to count how many conditions hold, but it hides the intent, so use it sparingly. The full set of conversion rules is in Type Conversion and Coercion.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these values is falsy?

    NaN is on the falsy list. Non-empty strings like "0" and "false" are truthy, and every array is truthy, even an empty one.
  2. 2. What does !!"hello" evaluate to?

    The first ! turns the truthy string into false; the second flips it to true.
  3. 3. How should you check whether the array items is empty?

    An empty array is truthy, and === compares arrays by reference, so only the length check works.
  4. 4. Why is if (!price) risky when price can be 0?

    0 is falsy, so the check can't tell a free item from an unknown price. Compare against undefined/null explicitly instead.
  5. 5. What does Boolean(new Boolean(false)) return?

    new Boolean(false) creates an object, and all objects are truthy. That's why you should never use new Boolean().

Summary

  • Booleans have two values, true and false; comparisons, ! and many methods produce them.
  • In conditions every value is either truthy or falsy.
  • The falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Everything else โ€” including "0", [] and {} โ€” is truthy.
  • Convert with Boolean(value) or !!value; never use new Boolean().
  • Truthiness checks are great shortcuts, but compare explicitly when 0, "" or false are valid data.

Next: null and undefined โ€” two ways JavaScript says "no value", and how to tell them apart.