Surya JS Journey

Learn JavaScript through concepts, code, and guided practice.

A focused workspace for concept study, interactive examples, visual explanations, and challenge solving.

Core JavaScriptBeginner

Truthy and Falsy

JavaScript treats some values as truthy and some as falsy when evaluating conditions.

Conditions in JavaScript do not require actual booleans. Values like `0`, `""`, `null`, `undefined`, `NaN`, and `false` are falsy, while most other values are truthy. This matters in conditionals, logical operators, and default-value patterns.

Explanation

Conditions in JavaScript do not require actual booleans. Values like `0`, `""`, `null`, `undefined`, `NaN`, and `false` are falsy, while most other values are truthy. This matters in conditionals, logical operators, and default-value patterns.

Key Points

  • Falsy values are limited and should be memorized.
  • An empty array and empty object are still truthy.
  • Truthiness is about conditional evaluation, not the original data type.

Common Mistakes

  • Assuming `[]` or `{}` are falsy because they look empty.
  • Using `||` for defaults when `0` or an empty string is a valid value.
  • Confusing `null` and `undefined` checks with generic falsy checks.

Practice Workspace

truthy-falsy.js

Edit the code, run it in the browser, and inspect the console output below. Reset restores the original snippet for this page.

Editor

9 lines

Output

0 entries

Mode

practice

Workspace Notes

Changes stay local until you run the code. Reset restores the original snippet immediately for another pass.

Live editing

Safe to change before every run.

Fast reset

Return to the original starter instantly.

truthy-falsy.js
9 linesMonaco Editor
Loading...

Editor Actions

Use the editor to explore the example, then run it to inspect the console.

Console0 entries

Run the code to see output here.

Expected Output

empty string is falsy
empty array is truthy

Continue in Playground

Open this concept example in the shared playground to keep experimenting without leaving the broader workspace flow.

Open in Playground