JavaScript Booleans

True or False

JavaScript Booleans

Booleans have only two values — `true` and `false`. But every value in JavaScript is "truthy" or "falsy" when used in a condition.

4 min read Level 1/5 #booleans#truthy#falsy
What you'll learn
  • Write boolean literals
  • Recognize the eight falsy values
  • Convert any value to a boolean

A boolean is either true or false. That’s the whole type.

Boolean literals script.js
const isActive = true;
const isLoggedIn = false;
const isAdult = 18 >= 18;     // comparison produces a boolean
console.log(isActive, isLoggedIn, isAdult);
▶ Preview: console

Comparison operators (===, >, <, etc.) and logical operators (!, &&, ||) produce booleans.

Truthy and Falsy

In an if condition, JavaScript converts whatever value you give it to a boolean. Most values become true — but a handful become false. Those are called falsy.

The complete list of falsy values:

ValueWhy it’s falsy
falseobviously
0zero
-0negative zero
0nbigint zero
""empty string
null”deliberately empty”
undefined”not assigned”
NaNnot a number

Everything else is truthy. Including:

  • "false" (string containing the word — truthy because it’s a non-empty string)
  • "0" (same — non-empty string)
  • [] (empty array — still an object)
  • {} (empty object)
  • Infinity
Truthy / falsy in conditions script.js
if (0)     console.log("0 truthy");       // not logged
if ("")    console.log("'' truthy");      // not logged
if (null)  console.log("null truthy");    // not logged
if ([])    console.log("[] truthy");      // ← logged! arrays are objects
if ({})    console.log("{} truthy");      // ← logged! objects are truthy
if ("0")   console.log("'0' truthy");     // ← logged! non-empty string
▶ Preview: console

Converting To a Boolean

Two ways:

Boolean conversion script.js
console.log(Boolean(""));         // false
console.log(Boolean("hello"));    // true
console.log(Boolean(0));          // false
console.log(Boolean(42));         // true
console.log(Boolean([]));         // true   ← still truthy
console.log(Boolean(null));       // false

// Double-bang — same as Boolean():
console.log(!!"hello");           // true
console.log(!!"");                // false
▶ Preview: console

The ! operator turns truthy → false and falsy → true. Apply it twice and you get a real boolean.

Common Patterns

Default value with ||

The || operator returns the first truthy operand:

Defaults with || script.js
const name = "" || "Anonymous";
console.log(name); // "Anonymous"  ← empty string is falsy

const count = 0 || 5;
console.log(count); // 5  ← but 0 is also falsy, which may be a bug
▶ Preview: console

The count example shows the pitfall: || treats 0, "", and false as “missing”. When you only want to substitute on null/ undefined, use ?? (nullish coalescing) — covered in the Control Flow chapter.

Boolean from condition

When you only care about a yes/no answer:

Coerce to boolean script.js
const items = [1, 2, 3];

const hasItems = !!items.length;     // true
console.log(hasItems);

const hasName = Boolean("Ada");      // true
console.log(hasName);
▶ Preview: console

Up Next

You can store single values. Now: storing collections of them.

JavaScript Arrays →