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;
trueandfalse: the only two boolean values, in lowercase and without quotes.name: the variable that holds the value.
See Boolean on MDN.
Prerequisites
- Node.js, to run the example. Install it from the Node.js download page.
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
| Mistake | What happens | Do 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 T | ReferenceError: True is not defined | Write it in lowercase |
Expecting Boolean("false") to be false | Any text that isn’t empty is truthy, so it gives true | Compare 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
- Boolean (MDN)
- typeof (MDN)
- Run Node.js scripts from the command line (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
