TExcelTAD
Home › JavaScript › JS Booleans

JavaScript Booleans

A boolean has only two possible values: true or false. Booleans are the result of every comparison and the input to every decision your program makes — if statements, loops and the logical operators all run on them.

js · try it yourself
const isAdult = 21 >= 18;
const hasPaid = false;
console.log(isAdult, hasPaid);
console.log(typeof isAdult);
console.log(10 > 9, 10 === "10", "a" !== "b");
true false
boolean
true false true

Truthy and falsy

JavaScript can treat any value as a boolean, for example in an if condition. Exactly eight values count as falsy; everything else is truthy:

Falsy values
false
0, -0
0n (BigInt zero)
"" (empty string)
null
undefined
NaN

Boolean(value) (or the double negation !!value) shows how a value is treated:

js · try it yourself
const samples = [0, 1, -1, "", " ", "0", "false", null, undefined, NaN, [], {}];
for (const v of samples) {
  console.log(JSON.stringify(v) ?? String(v), "->", Boolean(v));
}
0 -> false
1 -> true
-1 -> true
"" -> false
" " -> true
"0" -> true
"false" -> true
null -> false
undefined -> false
null -> false
[] -> true
{} -> true

Surprises to remember: the strings "0" and "false" are truthy (they're non-empty), and so are empty arrays and objects. (JSON.stringify writes NaN as null, which is why the tenth line looks like a second null.)

Booleans in conditions

Because of truthiness, you can test a value directly:

js · try it yourself
function greet(name) {
  if (name) {
    console.log(`Hello, ${name}!`);
  } else {
    console.log("Hello, whoever you are!");
  }
}
greet("Sana");
greet("");
greet();

const cart = [];
console.log(cart.length ? "Items in cart" : "Cart is empty");
Hello, Sana!
Hello, whoever you are!
Hello, whoever you are!
Cart is empty

Tip: Be careful when 0 or "" are valid values. if (count) is false when count is 0. Write the condition you mean: if (count !== undefined) or use ?? for defaults.

Logical operators

Operator Name true when…
a && b AND both are truthy
a || b OR at least one is truthy
!a NOT a is falsy

&& and || short-circuit: they stop as soon as the result is known, and they return one of the operands rather than a strict boolean:

js · try it yourself
console.log(true && "yes");     // && returns the last value if all are truthy
console.log(0 && "never");      // ...or the first falsy one
console.log("" || "default");   // || returns the first truthy value
console.log(null || 0 || "x");

const user = { name: "Ari" };
const loggedIn = true;
loggedIn && console.log("Welcome back,", user.name);   // runs only if loggedIn
yes
0
default
x
Welcome back, Ari

Naming booleans

Name boolean variables so they read like yes/no questions: isVisible, hasErrors, canSubmit, shouldRetry. Then conditions read naturally: if (isVisible && !hasErrors).

Note: new Boolean(false) creates an object, and every object is truthy — so if (new Boolean(false)) runs! Never use the Boolean constructor with new; call Boolean(x) as a function instead.

Test yourself

Which of these values is truthy?

  1. 0
  2. ""
  3. "0"
  4. null

What does null || "guest" evaluate to?

  1. null
  2. true
  3. "guest"
  4. false

What is Boolean([])?

  1. true
  2. false
  3. undefined
  4. TypeError