The JavaScript Boolean object
A Boolean is one of two values: true or false. In conditions, every value is treated as truthy or falsy. The six falsy values are false, 0, "", null, undefined and NaN; everything else is truthy. Convert any value to a real boolean with Boolean(x) or the double-bang !!x.
Overview
The Boolean type has just two values, true and false, and it underpins every decision your code makes: if statements, loops, comparisons. The comparison and logical operators (===, >) and ! produce booleans, though && and || return one of their operands rather than a boolean.
The concept that matters most in practice is truthy and falsy. When a non-boolean value is used where a boolean is expected (like an if condition), JavaScript coerces it. The falsy values worth memorizing are false, 0 (along with -0 and 0n), "" (empty string), null, undefined and NaN. Everything else is truthy, including "0", "false", [] and {}, which surprises people. That's why if (value) is a common shorthand for "value is present and non-empty".
To turn any value into an actual boolean, use Boolean(value) or the idiomatic double-NOT, !!value; both give true for truthy values and false for falsy ones. Avoid new Boolean() (the object wrapper), which is a footgun because a Boolean object is always truthy, even one wrapping false. Stick to the primitive.
Syntax
true; false;
// the six falsy values
Boolean(false); // false
Boolean(0); // false
Boolean(""); // false
Boolean(null); // false
Boolean(undefined); // false
Boolean(NaN); // false
!!"hello"; // true (convert with double-NOT)
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const values = [0, '', 'hi', [], null, '0'];
const lines = values.map(v =>
JSON.stringify(v) + ' -> ' + Boolean(v)
);
document.getElementById('out').textContent = lines.join('\n');
</script>
Best practices
- Memorize the six falsy values:
false, 0, "", null, undefined, NaN. Everything else is truthy. - Convert to a real boolean with
Boolean(x)or!!x. - Remember
"0",[]and{}are all truthy. - Never use
new Boolean(). The object wrapper is always truthy.
Frequently asked questions
What values are falsy in JavaScript?
false, 0 (and -0 and 0n), "", null, undefined and NaN. All other values are truthy.How do I convert a value to a boolean?
Boolean(value) or the double-NOT idiom !!value.Is an empty array falsy?
[] and {} are both truthy, even though they're "empty". Only the specific falsy values listed above are falsy.Why is "false" (a string) truthy?
"" is falsy, regardless of its contents.