Programming SearchโŒ˜K

Featureintro

Booleans and truthiness

Also called boolean, true, false, truthy, falsy, ToBoolean, Boolean(), !!x, boolean context.

The Boolean type has true and false. In a condition, ToBoolean turns any value into one of them: false, 0, -0, 0n, NaN, the empty string, null and undefined are falsy, and all other values are truthy.

Language
JavaScript

Synopsis

Falsy value Type
false Boolean
0, -0 Number
0n BigInt
NaN Number
'' String
null Null
undefined Undefined

The ToBoolean steps: "If argument is a Boolean , return argument . If argument is one of undefined , null , +0 ๐”ฝ , -0 ๐”ฝ , NaN , 0 โ„ค , or the empty String, return false ." Else "Return true ."1 The web host adds document.all as a falsy object.1 The types in the table are from the MDN list of falsy values.2

const values = [false, 0, -0, 0n, NaN, "", null, undefined, "0", "false", [], {}, -1];
console.log(values.map((v) => Boolean(v)).join(" "));
Output
false false false false false false false false true true true true true

Semantics

r1. Two values. "Boolean values can be one of two values: true or false , representing the truth value of a logical proposition."3 They "are typically produced by relational operators , equality operators , and logical NOT ( ! )".3

r2. Boolean contexts. "JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it, such as conditionals and loops ."2 So if, while, ? :, !, && and || all use ToBoolean on a value of any type. Example: contexts.

r3. The falsy values. "All values are truthy unless they are defined as falsy . That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , NaN , and document.all ."4 Example: synopsis.

r4. All objects are truthy. In the Boolean coercion list, "All objects become true ."3 So [], {} and new Boolean(false) are truthy. Strings with text, such as '0' and 'false', are also truthy.4 Example: synopsis.

r5. Explicit conversion. "For converting non-boolean values to boolean, use Boolean as a function or use the double NOT operator. Do not use the Boolean() constructor with new ."3 Example: convert.

r6. && and || keep the operands. "Note that binary logical operators such as && and || return the values of the operands, which may or may not be boolean values."3 Example: contexts.

Examples

The examples run with Node.js 24.

Conditions and logical operators (rules r2 and r6):

const items = [];
if (items) {
  console.log("an empty array is truthy");
}
if (!items.length) {
  console.log("but its length is 0");
}
console.log(true && "dog", [] && "dog", 0 || "fallback", "" ? "yes" : "no");
Output
an empty array is truthy
but its length is 0
dog dog fallback no

Boolean(), !! and new Boolean() (rule r5):

const x = "";
console.log(Boolean(x), !!x, typeof Boolean(x));
const wrapped = new Boolean(false);
console.log(typeof wrapped, Boolean(wrapped), wrapped ? "truthy" : "falsy");
Output
false false boolean
object true truthy

A falsy test drops a valid 0. ?? tests only for null and undefined:

function withDefault(count) {
  return [count || 10, count ?? 10];
}
console.log(withDefault(0), withDefault(undefined), withDefault(5));
Output
[ 10, 0 ] [ 10, 10 ] [ 5, 5 ]

Pitfalls

0 and '' are real values

count || 10 replaces 0 with 10 (zero). Use ?? when 0 and '' are valid values. See Nullish coalescing operator.

Empty arrays and objects are truthy

if ([]) runs its body (rule r4). Test arr.length for an empty array.

'false' is truthy

Any string with text is truthy, including 'false' and '0'.4

Comparison

Topic JavaScript Python
boolean values true, false3 True, False5
zero numbers falsy4 "zero of any numeric type" is false5
empty string falsy4 false5
empty array or list truthy (rule r4) "empty sequences and collections" are false5
custom rule none, all objects are truthy3 __bool__() or __len__()5

In Python, "an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero".5 So an empty list is false in Python and an empty array is true in JavaScript. See Truth value testing.

References

  1. ECMAScript 2026 Language Specification, 7.1.2 ToBoolean
  2. Falsy, MDN Glossary, introduction; Examples
  3. Boolean, MDN, introduction; Description; Boolean primitives and Boolean objects; Boolean coercion
  4. Truthy, MDN Glossary, introduction
  5. Built-in Types, The Python Standard Library, Truth Value Testing

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source