true in JavaScript: the true value of the boolean type

true is one of the two boolean values in JavaScript. What produces it, what merely looks like it, and how to test it without falling into traps.
3 min read
Believemy logo

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.

JAVASCRIPT
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.

Good to know

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.

ValueIn a testEqual to true with ===?
trueTrueYes
1TrueNo
"0"True, the string is not emptyNo
[]True, an array is still an objectNo
"false"True, it is textNo

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.

JAVASCRIPT
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 test

The 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

Question

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 (===).


Question

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.


Question

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.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.