Site icon JD Bots

How to Use true and false (Booleans) in JavaScript

A card titled true and false, labelled JavaScript Basics, showing isOpen as true, isFull as false, 10 greater than 5 as true, and typeof giving boolean.

true and false: JavaScript’s yes-or-no values, called booleans.

A boolean holds one of two values, true or false. JavaScript uses booleans for yes-or-no settings, and every comparison, such as 10 > 5, gives one back.

Every command and output on this page is real, run with Node.js 26.13.1 on macOS.

Syntax

Syntax

const name = true;
const name = false;

See Boolean on MDN.

Prerequisites

The Example

Two settings for a shop, isOpen and isFull, a comparison, and a check of the type. The file is shop.js.

Step 1: Write the Code

shop.js

const isOpen = true;
const isFull = false;

console.log(isOpen);
console.log(isFull);
console.log(10 > 5);
console.log(typeof isOpen);

typeof gives the name of a value’s type.

Step 2: Run It

Terminal

node shop.js

Output

true
false
true
boolean

The two settings print as they are, 10 > 5 gives true, and typeof shows that it’s a boolean.

Common Mistakes

MistakeWhat happensDo this instead
Putting quotes around it: "true"It’s text, not a boolean: typeof "true" is "string"Write true without quotes
Writing True with a capital TReferenceError: True is not definedWrite it in lowercase
Expecting Boolean("false") to be falseAny text that isn’t empty is truthy, so it gives trueCompare the text: text === "true"

FAQ

Where do booleans come from besides true and false?

From comparisons such as >, === and !==, and from operators such as !.

Conclusion

Write true and false in lowercase without quotes for yes-or-no values. Comparisons give you booleans too.

Additional Resources

JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.

Exit mobile version