Nearby lessons
5 of 22Advanced JavaScript: Truthy and Falsy Values
📌 What are Truthy and Falsy Values?
In JavaScript, every value has an inherent truthy or falsy quality when used in a boolean context like if statements or logical operations.
Understanding truthy and falsy values helps in writing cleaner conditionals, avoiding unexpected bugs, and mastering logical flow in JavaScript.
1️⃣ What are Falsy Values?
Falsy values are treated as false when evaluated in a boolean context.
| Attribute | Value | Description |
|---|---|---|
`false` | false | Boolean false |
`0` | 0 | Zero (number) |
`-0` | -0 | Negative zero |
`''` | '' | Empty string |
`null` | null | Absence of any value |
`undefined` | undefined | Variable declared but not defined |
`NaN` | NaN | Not a number |
Example02
2️⃣ What are Truthy Values?
Truthy values are all values that are not falsy. They evaluate to true in boolean conditions.
| Attribute | Value | Description |
|---|---|---|
`true` | true | Boolean true |
`{}` | {} | Non-empty object (also empty object) |
`[]` | [] | Empty array (considered truthy) |
`' '` | ' ' | Non-empty string (even with a space) |
`42`, `-1`, etc. | Any non-zero number | Numbers other than 0 are truthy |
`Infinity`, `-Infinity` | Infinity, -Infinity | Special numeric values treated as truthy |
Example03
3️⃣ Practical Use in if Conditions
Falsy values can lead to unexpected results if not handled properly in if statements.
Example04
4️⃣ Use of Double Negation (!!) to Convert to Boolean
You can use !!value to convert any value to a boolean explicitly.
Example05
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3