Surya JS Journey
Learn JavaScript through concepts, code, and guided practice.
A focused workspace for concept study, interactive examples, visual explanations, and challenge solving.
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.
Editor Actions
Use the editor to explore the example, then run it to inspect the console.
Run the code to see output here.
Expected Output
empty string is falsy
empty array is truthyContinue in Playground
Open this concept example in the shared playground to keep experimenting without leaving the broader workspace flow.
Open in PlaygroundRelated Concepts
Data Types
JavaScript has primitive and reference data types, and knowing the difference affects comparison, copying, and mutation.
Type Coercion
Type coercion is JavaScript converting one value type into another during operations or comparisons.
Equality
JavaScript has loose and strict equality, and they behave differently because loose equality allows coercion.