JavaScript · Lesson 08 / 32 · English
Booleans
Understand the idea. Change the code. Build confidence with one focused experiment.
Read this lesson in தமிழ் ↗.
Start with the idea
A boolean is true or false. Use comparisons to give business rules a clear name, then use that boolean in a decision.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
Step-by-step lesson notes
Booleans in JavaScript
A boolean represents one of two values: true or false. Booleans are used in conditions, comparisons, and logical operations.
Boolean Values
let isLoggedIn = true;
let hasErrors = false;
// Boolean() converts any value to true/false
Boolean(1); // true
Boolean(0); // false
Boolean("hello"); // true
Boolean(""); // false
Boolean(null); // false
Boolean(undefined); // false
Boolean([]); // true ← empty array is truthy!
Boolean({}); // true ← empty object is truthy!Falsy and Truthy Values
Every value in JavaScript is either truthy or falsy when used in a boolean context (like an if condition).
| Falsy Values (only 8) | Truthy (everything else) |
|---|---|
false | Any non-empty string: "hello", "false" |
0 | Any non-zero number: 1, -1, 3.14 |
-0 | Empty array: [] |
0n (BigInt zero) | Empty object: {} |
"" (empty string) | Any function |
null | |
undefined | |
NaN |
// Falsy in if conditions
if (0) { /* won't run */ }
if ("") { /* won't run */ }
if (null) { /* won't run */ }
if (undefined) { /* won't run */ }
// Truthy in if conditions
if ("0") { /* WILL run — non-empty string */ }
if ([]) { /* WILL run — empty array is truthy! */ }
if ({}) { /* WILL run — empty object is truthy! */ }
// Double negation trick — convert to boolean
const hasValue = !!someVariable;
console.log(!!"hello"); // true
console.log(!!""); // false
console.log(!!0); // false
console.log(!!null); // falseCommon gotcha: [] and {} are truthy even though they're empty. To check if an array is empty, use arr.length === 0, not just if (arr).
A complete example
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
const age = 18;
const hasTicket = true;
const canEnter = age >= 18 && hasTicket;
console.log(canEnter);
console.log(Boolean("false"));Predict first. Reveal the expected result.
true true
Make one change. See what happens.
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Wait for the editor to load, then load this example and press Run.
Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.
Put the idea to work
Practice challenge
Set hasTicket to false. Explain why the first result changes.
Reveal the answer and reasoning
canEnter becomes false because both conditions must be true for &&.
One mistake to avoid: The non-empty string "false" is truthy. It is not the boolean false.
Your learning checkpoint
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.