JavaScript Booleans
You already cut and search strings. Programs also need a clear yes or no — is the form ready? Did the search find a match? That answer type is a boolean: only true or false. This session stays with the value itself; full if branching comes later.
What is a boolean in JavaScript?
A boolean is a yes/no value. The two keywords are true and false — no quotes. typeof true is "boolean". You can store a flag (let ready = true), get a boolean from a comparison (age >= 18), or convert another value with Boolean(value). Strings like "true" are still text, not booleans. A short falsy list (values that become false when converted) helps you avoid surprises with 0, "", null, and undefined.
Boolean toolkit
📊 Everyday boolean ideas beginners meet first
| Idea | Example | Result / note |
|---|---|---|
| Literal yes | true |
keyword, not a string |
| Literal no | false |
keyword, not a string |
typeof |
typeof false |
"boolean" |
| Comparison | 5 > 3 |
true |
| Convert | Boolean(0) |
false (common falsy number) |
| Convert text | Boolean("hi") |
true (non-empty string) |
Write true and false
Use the keywords true and false. Log the value and typeof so you see the type label "boolean".
let ready = true;
let done = false;
console.log(ready);
console.log(done);
console.log(typeof ready);
console.log(typeof done);
Comparisons return a boolean
Operators like >, <, >=, <=, ===, and !== answer with true or false. You do not invent the boolean by hand every time — the comparison produces it.
console.log(10 > 5);
console.log(2 === 2);
console.log(2 === "2");
console.log(7 < 3);
Convert with Boolean()
Boolean(value) turns another value into true or false. Empty-ish values become false; many everyday values become true.
console.log(Boolean(1));
console.log(Boolean(0));
console.log(Boolean("hello"));
console.log(Boolean(""));
A short falsy map
These become false when passed to Boolean() (or used later in a condition): false, 0, "", null, undefined, and NaN. Almost everything else beginners try first is truthy (including "0" and "false" as non-empty strings).
console.log(Boolean(false));
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean(undefined));
Truthy values that surprise beginners
Non-empty strings are truthy — even "0" and "false". The number 1 is truthy too. Log these so the contrast with falsy sticks.
console.log(Boolean("0"));
console.log(Boolean("false"));
console.log(Boolean(1));
console.log(Boolean(" "));
Show a yes/no flag on the page
Store a boolean, then put a short message on the page with textContent.
<!DOCTYPE html>
<html>
<head>
<title>Booleans — ready flag</title>
</head>
<body>
<p id="out">…</p>
<script>
let ready = true;
document.getElementById("out").textContent = "ready = " + ready;
</script>
</body>
</html>
The paragraph shows the boolean on the page (ready = true or ready = false). Flip only ready, refresh, and confirm the text updates. Later lessons use this flag inside full if checks.
Try it yourself
- Create
let on = trueandlet off = false; log both values and bothtypeofresults. - Log
8 >= 8,3 !== 4, and1 === "1"; predict each result before you run. - Call
Booleanon1,0,"ok", and""; write down which aretrue. - Log
Boolean("0")andBoolean(0)— notice the string is truthy, the number zero is falsy. - In the HTML demo, set
readytofalse, refresh, and read the new line. - Change the label text before
+ readyto your own short word, keep the same boolean.
Common mistakes
- Writing
"true"or"false"with quotes — those are strings, not booleans - Using
True/Falsewith capital letters — JavaScript keywords are lowercasetrue/false - Thinking
1and0are the boolean type — they are numbers; conversion can map them, buttypeof 1is"number" - Ignoring that comparisons already return a boolean — you often store the result:
let ok = score >= 10 - Treating the non-empty string
"false"as falsy —Boolean("false")istrue
Summary
- A boolean is only
trueorfalse(keywords, no quotes) typeofreports"boolean"; comparisons like===and>return a booleanBoolean(value)converts; remember common falsy values:0,"",null,undefined,false,NaN- Strings
"true"/"false"are text — not the boolean type - Next:
nullandundefined— two “empty” ideas that are not booleans
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top