FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Values & Types

Intermediate

Truthy & Falsy

Truthy and falsy describe how values behave when JavaScript needs a boolean, such as inside if, while, &&, || or !.

if ("0") {
  console.log("This runs because non-empty strings are truthy.");
}

if (0) {
  console.log("This does not run.");
}

Values & Types

Truthy and falsy are boolean conversion rules.

JavaScript does not require every condition to be true or false already. It converts values to boolean in conditional contexts.

Only a small list of values is falsy: false, 0, -0, 0n, an empty string, null, undefined, NaN and the browser legacy object document.all. Everything else is truthy.

This is convenient, but it can hide bugs. An empty array is truthy. An empty object is truthy. The string "false" is truthy because it is not empty.

Falsy values

Values that become false in boolean context. The list is small and should be memorized.

Truthy values

All values not on the falsy list, including [], {}, "0" and "false".

Boolean()

A clear way to convert a value to true or false.

Guard checks

Useful, but only when you mean all falsy values are invalid.

Examples

Good type handling makes JavaScript predictable.

Explicit check when 0 is valid

const itemCount = 0;

if (itemCount !== null && itemCount !== undefined) {
  console.log(`Items selected: ${itemCount}`);
}

Falsy check rejects valid values

const itemCount = 0;

if (!itemCount) {
  console.log("No item count provided");
}

// But 0 may be a real value.

Rules that matter

Types are not paperwork. They are behavior.

JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.

Memorize the falsy list

There are few falsy values; everything else is truthy.

Do not assume empty objects are false

{} and [] are truthy.

Use explicit checks for valid zero

If 0 is meaningful, do not reject it with !value.

Use Boolean for readability

Boolean(value) makes conversion obvious.

Use ?? for nullish fallback

Use || only when all falsy values should fallback.

Name boolean state clearly

isOpen, hasPaid and canSubmit should already read like true or false.

Production thinking

Data quality starts with value quality.

Why does this matter?

Truthy and falsy rules explain why JavaScript conditions sometimes feel surprising. Once you know the list, the surprises become predictable.

Accessibility

Form validation often uses truthy/falsy checks. Bad checks can reject legitimate answers or hide important feedback.

Production note

Production validation should distinguish empty, missing, zero and false when those states mean different things.

SEO note

Falsy rendering mistakes can accidentally hide valid content such as count 0, rating 0 or empty but meaningful labels.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Add "0", -0 and 0n to the values array.
  • Add an empty array and an empty object if they are not already there.
  • Change the output to show typeof value as well.

Practice assignment

Do this before moving to the next topic.

  1. Write the falsy list from memory.
  2. Create a condition where 0 should be accepted.
  3. Rewrite a !value check into a more explicit check.

Try it yourself

Test truthy and falsy values

Live preview

Self-check

Before you continue, prove that you understand Truthy & Falsy.

Intermediate

Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.

  1. Can you name the falsy values?
  2. Can you explain why [] is truthy?
  3. Can you explain why "false" is truthy?
  4. Can you explain when !value is too broad?
  5. Can you explain why ?? is often safer than || for defaults?