A program spends its life deciding. Does this cart hold anything, has this user paid, is this field filled in? Answering requires a data type that knows exactly two answers.
That is the boolean type, and true is its positive answer.
Definition
true is a literal of the language, one of only two possible boolean values alongside false. It is written in lowercase, without quotes, and it is a reserved word that cannot be reused as a variable name.
You get it two ways: by writing it by hand, or by letting a comparison produce it. The second is by far the more common.
const isAdmin = true; // written by hand
const cart = [1, 2, 3];
console.log(cart.length > 0); // true, produced by the comparison
console.log(cart.includes(2)); // true, produced by a method
console.log(typeof true); // "boolean"Remember the second half: every comparison returns a boolean and nothing else. So you never need to manufacture true in order to write a condition, the test handles it.
The pattern if (isActive === true) is redundant. if (isActive) says the same thing more briefly, and it is the convention across the whole language.
True, and what passes for true
An if does not demand a real boolean. It accepts any value, then decides for itself whether it counts as true. So the value true belongs on one side, and everything the language treats as true without being a boolean on the other.
The rule is easiest to remember backwards: Truthy and falsy lists the eight values considered false, and everything else is true. Including some values that surprise people.
| Value | In a test | Equal to true with ===? |
|---|---|---|
true | True | Yes |
1 | True | No |
"0" | True, the string is not empty | No |
[] | True, an array is still an object | No |
"false" | True, it is text | No |
The last three rows cause real damage. An empty array passes a test while holding nothing, and a "false" string read from a URL or an HTML attribute is perfectly true.
Converting to a boolean
Turning any value into a real boolean has two equivalent forms.
console.log(Boolean("0")); // true
console.log(!!"0"); // true, the short form
console.log(Boolean("")); // false
console.log(Boolean(0)); // false
// The trap of a string coming from elsewhere
const raw = "false";
console.log(Boolean(raw)); // true
console.log(raw === "true"); // false, and this is the right testThe double exclamation mark is not a special operator: it is negation applied twice, which reduces a value to its boolean equivalent. The explicit conversion through Boolean stays more readable in shared code.
Frequently asked questions
Why does 2 == true return false?
Because Loose equality (==) does not compare truthiness, it converts. true becomes the number 1, then 2 is compared with 1, and the result is false. That is exactly why comparing a value against true is discouraged: state the condition directly instead, or reach for Strict equality (===).
How do I read a boolean sent by a server?
If the data arrives as JSON, it is already a real boolean after parsing and there is nothing to do. If it arrives in a URL or an HTML attribute, it is necessarily text: compare explicitly with value === "true", since any non-empty string would pass a raw test.
Should I use new Boolean(true)?
No, never. That form creates a wrapper object, and an object is always true in a test, including new Boolean(false), which produces the exact opposite of the expected behavior. Use the function without new, which does return a primitive boolean.