Loading...

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

true and false literals

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

Comparisons give true or false

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

Boolean conversion

javascript
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).

Falsy values to remember

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

Truthy surprises

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

Page shows a ready flag

html
<!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

  1. Create let on = true and let off = false; log both values and both typeof results.
  2. Log 8 >= 8, 3 !== 4, and 1 === "1"; predict each result before you run.
  3. Call Boolean on 1, 0, "ok", and ""; write down which are true.
  4. Log Boolean("0") and Boolean(0) — notice the string is truthy, the number zero is falsy.
  5. In the HTML demo, set ready to false, refresh, and read the new line.
  6. Change the label text before + ready to your own short word, keep the same boolean.

Common mistakes

  • Writing "true" or "false" with quotes — those are strings, not booleans
  • Using True / False with capital letters — JavaScript keywords are lowercase true / false
  • Thinking 1 and 0 are the boolean type — they are numbers; conversion can map them, but typeof 1 is "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") is true

Summary

  • A boolean is only true or false (keywords, no quotes)
  • typeof reports "boolean"; comparisons like === and > return a boolean
  • Boolean(value) converts; remember common falsy values: 0, "", null, undefined, false, NaN
  • Strings "true" / "false" are text — not the boolean type
  • Next: null and undefined — two “empty” ideas that are not booleans

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
8
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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