JavaScript Booleans: true, false, Truthy and Falsy Values
The boolean type and the truthiness rules every JavaScript developer must know: the complete list of falsy values, surprising truthy ones ("0", [], {}), converting with Boolean() and !!, when if (value) is a bug, booleans in arithmetic, and the new Boolean(false) trap.
kvi5hnuCode Sprout team member5 min readOn this page
The boolean type has exactly two values, true and false. They're the result of comparisons (age >= 18, name === "Ana") and the input to every decision your program makes: if, while, ?:, &&, || and !.
What makes booleans interesting in JavaScript is that conditions accept any value, not only booleans. Each value counts as either "truthy" or "falsy". Knowing exactly which values are falsy prevents a whole family of bugs.
Comparisons produce booleans
const age = 20, name = "Ana";
console.log(age >= 18); // comparison operators
console.log(name === "Ana");
console.log(name.startsWith("B")); // many methods return booleans
console.log(Array.isArray([]));
console.log(typeof true);
true
true
false
true
boolean
The boolean operators combine them: && (and), || (or) and ! (not). Part 4 covers them in detail, including the important fact that && and || return one of their operands, not necessarily a boolean.
if ((hasTicket && age >= 18) || isStaff) {
// allowed in
}
Truthy and falsy
When JavaScript needs a boolean (in an if, a loop condition, !, &&, || or ?:), it converts whatever value it has. Only eight values are falsy. Every other value, without exception, is truthy:

const values = [false, 0, -0, 0n, "", null, undefined, NaN,
"0", "false", " ", [], {}, () => {}, -1, Infinity];
for (const value of values) {
const label = typeof value === "string" ? JSON.stringify(value)
: typeof value === "function" ? "() => {}"
: Array.isArray(value) ? "[]"
: typeof value === "object" && value !== null ? "{}"
: Object.is(value, -0) ? "-0"
: typeof value === "bigint" ? value + "n"
: String(value);
console.log(label.padEnd(10), value ? "truthy" : "falsy");
}
false falsy
0 falsy
-0 falsy
0n falsy
"" falsy
null falsy
undefined falsy
NaN falsy
"0" truthy
"false" truthy
" " truthy
[] truthy
{} truthy
() => {} truthy
-1 truthy
Infinity truthy
The surprises are on the truthy side:
"0"and"false"are truthy. They're non-empty strings. Form fields and URL query parameters are strings, soif (params.get("debug"))is true for?debug=false.- Empty arrays and objects are truthy. Every object is truthy, regardless of its contents. To test for an empty array, check
array.length; for an empty object,Object.keys(obj).length. - Wrapper objects are truthy, even
new Boolean(false)(see below).
Converting to a boolean
To get an actual true or false, call Boolean(value) or use double negation !!value (the first ! converts and negates, the second negates back). Both follow the same rules:
console.log(Boolean("hello"), !!"hello");
console.log(Boolean(0), !!0);
console.log(Boolean("false")); // non-empty string: true!
console.log([1, 0, "x", "", null, 7].filter(Boolean)); // a common idiom
true true
false false
true
[ 1, 'x', 7 ]
array.filter(Boolean) uses the Boolean function as the test, keeping only truthy elements. It's a neat way to drop empty strings, nulls and undefineds, but it also drops 0 and NaN, which might be real data.
To parse text that represents a boolean, compare explicitly, because Boolean("false") is true:
function parseFlag(text) {
const normalized = String(text).trim().toLowerCase();
if (["true", "1", "yes", "on"].includes(normalized)) return true;
if (["false", "0", "no", "off", ""].includes(normalized)) return false;
throw new Error(`Not a boolean: ${text}`);
}
console.log(parseFlag("false"), parseFlag(" TRUE "), parseFlag("on"));
console.log(JSON.parse("false")); // JSON text uses the literal words
false true true
false
When if (value) is a bug
Using truthiness as a shortcut for "is there a value?" is idiomatic and fine when the falsy values can't be valid data. It's a bug when they can:
function describeStock(count) {
if (!count) return "unknown"; // bug: 0 is a real stock level
return `${count} in stock`;
}
console.log(describeStock(5), "|", describeStock(0), "|", describeStock(undefined));
function describeStockFixed(count) {
if (count == null) return "unknown"; // only null and undefined
return `${count} in stock`;
}
console.log(describeStockFixed(5), "|", describeStockFixed(0), "|", describeStockFixed(undefined));
5 in stock | unknown | unknown
5 in stock | 0 in stock | unknown
The same trap applies to defaults: const volume = settings.volume || 50 replaces a deliberate volume of 0 with 50. The nullish coalescing operator ?? (ES2020) only falls back for null and undefined: settings.volume ?? 50. Rules of thumb:
| You want to check | Write |
|---|---|
| "is it null or undefined?" | value == null or value ?? fallback |
| "is it a non-empty string?" | typeof s === "string" && s.length > 0 |
| "does the array have items?" | arr.length > 0 |
| "is it a usable number?" | Number.isFinite(n) |
| "is it any value that could be data?" | not if (value)! |
Booleans in arithmetic and as strings
In numeric contexts true becomes 1 and false becomes 0, which can be handy for counting:
console.log(true + true, true * 10, +false);
const answers = [true, false, true, true];
console.log(answers.filter(Boolean).length); // count the trues
console.log(answers.reduce((sum, ok) => sum + ok, 0)); // the same, via arithmetic
console.log(String(true), `${false}`, true + "!"); // as text
2 10 0
3
3
true false true!
The filter(...).length version states the intent more clearly than adding booleans.
Don't use new Boolean
Boolean(x) without new returns a primitive. With new, it creates a wrapper object, and objects are always truthy:
const flag = new Boolean(false);
console.log(typeof flag); // an object, not a boolean
console.log(flag ? "runs" : "skipped"); // objects are truthy!
console.log(flag.valueOf()); // the wrapped value
console.log(flag == false, flag === false); // loose equality unwraps, strict doesn't
object
runs
false
true false
There's no reason to write new Boolean, new Number or new String in application code. ESLint's no-new-wrappers rule reports them.
Common questions
Is an empty array truthy or falsy?
Truthy, like every object. But [] == false is true, because == converts the array to a primitive ("") and then to a number (0). This is one of the reasons to prefer ===; article 23 walks through the algorithm.
Is if (x) the same as if (x == true)?
No. if (x) checks truthiness. x == true converts true to 1 and compares numerically, so "2" == true is false while "2" is truthy, and "1" == true is true. Never compare to true or false with ==.
What's the difference between !!x and Boolean(x)?
None in result. Boolean(x) is more readable to newcomers; !!x is shorter and common in existing code. Pick one style per codebase.
Is NaN falsy?
Yes. So is -0, and bigint 0n. Use Number.isNaN when you need to tell NaN apart from 0.
Summary
booleanhas two values,trueandfalse, produced by comparisons and many methods.- Conditions convert any value. The eight falsy values are
false,0,-0,0n,"",null,undefinedandNaN; everything else is truthy, including"0","false",[]and{}. - Convert with
Boolean(x)or!!x; parse boolean text by comparing strings, not withBoolean. if (value)andvalue || defaulttreat0and""as missing; use== nulland??when those are valid data.trueandfalseact as 1 and 0 in arithmetic. Never usenew Boolean.
Practice Questions
Which of these are falsy: "0", 0, [], "", NaN, "null", 0n, -0?
Answer
0, "", NaN, 0n and -0. The strings "0" and "null" are non-empty, and [] is an object, so they're truthy.
What happens for page=0, page=abc and no page parameter?
const page = Number(query.page) || 1;
Answer
All three give 1. Number("0") is 0 (falsy), Number("abc") is NaN (falsy), and a missing parameter gives Number(undefined), which is NaN (or Number(null), which is 0, if it came from URLSearchParams.get): falsy either way. If page 0 is valid, check with Number.isInteger instead.
What does [0, "", "a", null, 2].filter(Boolean) return?
Answer
["a", 2].
Why is new Boolean(false) ? 1 : 2 equal to 1?
Answer
new Boolean(false) is an object, and all objects are truthy, regardless of the value they wrap.
Rewrite this code so that only a missing value gets the default, but an empty string also counts as missing.
const name = input || "Anonymous";
Answer
const name = input == null || input === "" ? "Anonymous" : input;
Or, more compactly, if input is always a string or missing:
const name = input?.length ? input : "Anonymous";
Previous: JavaScript Template Literals: Interpolation, Multi-line Strings, Tagged Templates and String.raw
Next: null and undefined in JavaScript: Differences, Checks and Best Practices

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.