JavaScript Booleans & Comparisons

true, false, truthy and falsy.

✍️ 3 exercises + code challenge in this lesson

A boolean is either true or false. Comparisons produce booleans.

Truthy and falsy

In conditions, every value acts as true or false. These values are falsy:

false, 0, "" (empty string), null, undefined, NaN

Everything else is truthy, including "0", [] and {}.

Key Takeaways: JavaScript Booleans & Comparisons

Mastering JavaScript Booleans & Comparisons helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript Booleans & Comparisons Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
log("10 > 9:", 10 > 9);
log("'apple' < 'banana':", "apple" < "banana");

const values = [0, 1, "", "hello", null, undefined, NaN, [], {}];
for (const v of values) {
  log(JSON.stringify(v) ?? String(v), "is", v ? "truthy" : "falsy");
}
log(Boolean("0"));
</script>
Free sign-in required · Google login available

JavaScript Booleans & Comparisons Practice Exercises

Check what you learned in “JavaScript Booleans & Comparisons”.

/ 3 solved
Fill in the blank

Convert any value to a boolean.

("hello")   // true
✓ Correct! Not quite. Try again.
Multiple choice

Which of these values is falsy?

✓ Correct! Not quite. Try again.
Code challenge

Log whether 10 is greater than 9 (it should print true).

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services