SKU // 21

JavaScript Booleans

A boolean is the simplest data type there is: it has exactly two possible values, true and false. Booleans are the language of decisions. Every if, every loop, and every comparison comes down to one of these two values.

Programs constantly need to represent one of two states:

// Boolean Values

let isOnline   = true;
let isLoggedIn = false;
console.log(typeof isOnline);
boolean

Two rules for writing them:

Naming tip
Name boolean variables so they read like a yes/no question: isOpen, hasTicket, canVote. Then if (canVote) reads almost like English.

// Booleans from Comparisons

You rarely type true or false directly. Most booleans come from comparisons, and every comparison operator gives one back. Given let x = 5;:

Description Example Returns
Equal tox === 8false
Not equal tox !== 8true
Greater thanx > 8false
Less thanx < 8true

You can store the result of a comparison in a variable, and use it later:

const score     = 85;
const passed    = score >= 60;
const excellent = score >= 90;

if (passed) {
  console.log("You passed! Excellent: " + excellent);
}
You passed! Excellent: false

// Where Booleans Are Used

Booleans drive every kind of decision in JavaScript. You have seen conditions; loops (coming up in the next section) use them too, to decide whether to keep repeating:

Where Example The boolean decides
ifif (age < 18)whether the block runs
ternaryhour < 18 ? "day" : "evening"which value is picked
for loopfor (let i = 0; i < 5; i++)whether to go around again
while loopwhile (lives > 0)whether to keep going

// The Boolean() Function

Boolean() converts any value to true or false. It is useful for checking how JavaScript will treat a value in a condition:

console.log(Boolean(10 > 9));
console.log(10 > 9);           // even easier: a comparison already is a boolean
console.log(Boolean("hello"));
console.log(Boolean(0));
true true true false

// Everything With a "Value" Is True

When JavaScript turns a non-boolean value into a boolean, anything that has a real "value" becomes true. These are called truthy:

100              // true
3.14             // true
-15              // true: any non-zero number, even negative
"Hello"          // true
"false"          // true: it is a non-empty string!
"0"              // true: also a non-empty string
" "              // true: a space is a character
(7 + 1 + 3.14)   // true: the result is 11.14
[]               // true: an empty array
{}               // true: an empty object

The last two surprise people. Arrays and objects are always truthy, no matter what is (or is not) inside them, because they are objects, and every object is truthy.

// Everything Without a "Value" Is False

There are exactly eight falsy values. Memorize this list; everything not on it is truthy.

Value Type Notes
falsebooleanThe boolean itself
0numberZero
-0numberMinus zero
0nbigintBigInt zero
""stringEmpty string only; " " (a space) is truthy
nullnullEmpty on purpose
undefinedundefinedA variable never given a value
NaNnumberFailed math, like 10 / "Hallo"
let x;                       // undefined
console.log(Boolean(x));
console.log(Boolean(10 / "Hallo"));   // NaN
console.log(Boolean(""));
false false false

// Booleans as Objects (Don't)

Normally a boolean is a simple primitive value: let x = false;. But JavaScript also lets you create a boolean object with the new keyword. You should never do this, but you should recognize it, because it behaves very strangely:

let x = false;
let y = new Boolean(false);

console.log(typeof x);
console.log(typeof y);
console.log(x == y);
console.log(x === y);

if (y) {
  console.log("This runs, even though y holds false!");
}
boolean object true false This runs, even though y holds false!
Watch out: new Boolean(false) is truthy
y is an object wrapping the value false, and every object is truthy. So if (y) runs, which is exactly backwards. Boolean objects also make code slower and cannot be compared reliably with ===. Never use new Boolean(). Plain Boolean(value) without new is fine: it returns a normal primitive boolean.

// Quick Check

Which of these is falsy?

  1. "false"
  2. []
  3. -1
  4. NaN
Show answer
D. NaN is one of the eight falsy values. "false" is a non-empty string, [] is an object, and -1 is a non-zero number, so all three are truthy.

What does this log?

let answer = "false";
if (answer) {
  console.log("yes");
} else {
  console.log("no");
}
Show answer
yes. "false" in quotes is a five-letter string, and any non-empty string is truthy. This happens in real code when a true/false setting is stored or sent as text.

Which is a correctly written boolean?

  1. let open = True;
  2. let open = "true";
  3. let open = true;
  4. let open = new Boolean(true);
Show answer
C. A is a ReferenceError (capital T makes it a variable name). B is a string. D is a Boolean object, which you should never create.

// Try It

  1. Click Open Playground. Run Boolean() on at least eight different values. Predict each answer before you click Run
  2. Confirm that " " (a space) is truthy but "" is falsy
  3. Store a comparison in a variable, const canVote = age >= 18;, and use it in an if
  4. Create new Boolean(false), put it in an if, and see it run. Then try the same with Boolean(false) (no new)
// Video Walkthroughs
Last updated: Oct 9, 2026, 11:53 PM ET