Booleans

</>Coding Tamilan.← JavaScript course

JavaScript · Lesson 08 / 32 · English

Booleans

Understand the idea. Change the code. Build confidence with one focused experiment.

Detailed notesRunnable JavaScriptPractice + answer

Read this lesson in தமிழ் ↗.

01 / Understand

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.

02 / Explore

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

JavaScript reference fragmentCODE
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)
falseAny non-empty string: "hello", "false"
0Any non-zero number: 1, -1, 3.14
-0Empty array: []
0n (BigInt zero)Empty object: {}
"" (empty string)Any function
null
undefined
NaN
JavaScript reference fragmentCODE
// 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);    // false

Common gotcha: [] and {} are truthy even though they're empty. To check if an array is empty, use arr.length === 0, not just if (arr).

03 / Read & predict

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.

index.js · complete exampleCODE
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
04 / Experiment

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.

Open editor ↗

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.

05 / Your turn

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.
Coding Tamilan · Learn by doing.