Boolean is a JavaScript primitive type with exactly two values: true and false. A primitive is a plain value, such as true, 42, or "hi", and not an object. Booleans answer yes-or-no questions: Is the user logged in? Is the cart empty? Is 5 bigger than 3?
Comparisons such as 5 > 3 give a Boolean, and if, while, and the ? : operator all use one to choose what happens next. The typeof of a Boolean is "boolean". Booleans have been part of JavaScript since its first versions, so they work in every browser and in every version of Node.js.
Write true and false in lowercase.
True and TRUE are not Booleans. JavaScript reads them as variable names and throws a ReferenceError.
Syntax
Three Ways to Get a Boolean
Write the literal true or false, compare two values, or convert any value with the Boolean() function. The third way, new Boolean(), makes an object, so do not use it. The comments show each result.
JavaScript
const a = true; // a Boolean literal
const b = Boolean(1); // conversion: a plain (primitive) Boolean
const c = new Boolean(false); // a wrapper object (avoid it)
typeof a; // "boolean"
typeof b; // "boolean"
typeof c; // "object"
5 > 3; // true (comparisons give Booleans)
Boolean(); // false (no argument)
True; // ReferenceError (the keyword is lowercase)
Gotcha
new Boolean(false) Is Truthy
A wrapper object is an object that holds a plain value inside. new Boolean(false) builds one. It looks like false but it is an object, and every object is truthy (truthy means “counts as true in an if”). So if (new Boolean(false)) runs its block. Boolean(false) without new is the plain false.
JavaScript
const flag = new Boolean(false);
typeof flag; // "object"
flag ? "truthy" : "falsy"; // "truthy" (an object is always truthy)
flag.valueOf(); // false
flag == false; // true
flag === false; // false
Boolean(false); // false (no new: a plain primitive)
Boolean(flag); // true
Boolean(flag.valueOf()); // false
To get the plain value out of a wrapper, call valueOf(). Better still, never create wrappers: use true, false, or Boolean(value).
Rules
Truthy and Falsy Values
Anywhere JavaScript expects a Boolean, it converts the value you give it. Falsy values become false. Truthy values become true. There are only eight falsy values:
“Not a number”, the result of a failed calculation.
Everything else is truthy. That includes surprises such as the text "false", the text "0", an empty array [], and an empty object {}. (Old browsers also treat the special document.all object as falsy. You will almost never meet it.)
A shorter way to write Boolean(value) is !!value: the first ! flips the value to a Boolean, and the second flips it back. To keep only the truthy items of an array, pass the function itself to Array filter(): list.filter(Boolean).
Compare
Comparing and Combining Booleans
=== checks the value and the type. == converts first, so 5 == "5" is true; prefer ===. && means and, || means or, and ! means not. && and || do not always return a Boolean: they return one of the two values you gave them, which is how name || "guest" picks a default.
JavaScript
5 > 3; // true
5 === "5"; // false (different types)
5 == "5"; // true (loose: converts first)
true && false; // false
true || false; // true
!true; // false
!!"text"; // true (double not is the same as Boolean(text))
"" || "guest"; // "guest" (|| gives the first truthy value)
"Asha" && "admin"; // "admin" (&& gives the last value when all are truthy)
0 && "never"; // 0 (&& stops at the first falsy value)
Convert
Booleans, Numbers, and Strings
In math, true counts as 1 and false as 0. When you join a Boolean with text, you get the words "true" or "false", which JavaScript makes with toString(). Going the other way needs care: the text "false" is truthy. Read text flags with text === "true", or parse JSON with JSON.parse().
JavaScript
true + 1; // 2
false + 1; // 1
true + true; // 2
Number(true); // 1
Number(false); // 0
Number("true"); // NaN (the word is not a number)
String(true); // "true"
true + ""; // "true"
true + "1"; // "true1"
JSON.parse("false"); // false
Number covers the other conversions in more detail.
Uses
Where Booleans Are Used
Flags, such as isLoggedIn, hasItems, or darkMode, that remember a yes-or-no answer.
Conditions in if, while, and ? :, which pick the next step.
Toggles, where flag = !flag flips a switch such as a menu or a theme.
Filters, where a function returns true to keep an item, as in Array filter().
Validation, where a check such as age >= 18 answers whether the data is acceptable.
One loop tests every value. The condition values[i] ? "truthy" : "falsy" converts each item to a Boolean first, with the same rules as if.
Only the eight falsy values fail. The number 0, the empty string, null, undefined, and NaN are falsy. -0, 0n, and false would be too.
Empty is not always falsy. The texts "0" and "false" are truthy because they are not empty. An empty array [] and an empty object {} are objects, and objects are always truthy.
Example 2 — Read a Flag That Was Saved as Text
Forms, URLs, and storage hand you strings. Convert them with Boolean() and see why that is wrong for the text "false".
A primitive and an object.real is a primitive, so typeof gives boolean. new Boolean(false) is an object, so it gives object.
Only the object runs the block. An if asks whether the value is truthy. An object always is, so boxed runs its block. The plain false does not.
Equality shows the difference.== unwraps the object before comparing, so it says true. === compares types too, so it says false. valueOf() returns the plain false.
Continue
Boolean Method Groups
Boolean has only two methods. Each one has a full tutorial with examples you can run.
A Boolean is a primitive type with exactly two values, true and false. It answers yes-or-no questions and drives if statements. typeof true is "boolean".
false, 0, -0, 0n, "" (the empty string), null, undefined, and NaN. Every other value is truthy, including "false", "0", [], and {}.
Boolean("false") is true because it only checks whether the string is empty, not what it says. To read a saved flag, compare the text: str === "true". JSON.parse("false") also gives the real false.
Boolean(false) is the plain primitive false. new Boolean(false) is an object with typeof "object", and every object is truthy, so if (new Boolean(false)) runs its block. Call valueOf() to get the plain false out of it.
It converts any value to a Boolean. The first ! turns the value into the opposite Boolean, and the second ! turns it back, so !!"hello" is true and !!0 is false. It is the same as Boolean(value).