Nearby lessons

5 of 22

Advanced 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.

AttributeValueDescription
`false`falseBoolean false
`0`0Zero (number)
`-0`-0Negative zero
`''`''Empty string
`null`nullAbsence of any value
`undefined`undefinedVariable declared but not defined
`NaN`NaNNot a number
Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ What are Truthy Values?

Truthy values are all values that are not falsy. They evaluate to true in boolean conditions.

AttributeValueDescription
`true`trueBoolean true
`{}`{}Non-empty object (also empty object)
`[]`[]Empty array (considered truthy)
`' '`' 'Non-empty string (even with a space)
`42`, `-1`, etc.Any non-zero numberNumbers other than 0 are truthy
`Infinity`, `-Infinity`Infinity, -InfinitySpecial numeric values treated as truthy
Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Practical Use in if Conditions

Falsy values can lead to unexpected results if not handled properly in if statements.

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Use of Double Negation (!!) to Convert to Boolean

You can use !!value to convert any value to a boolean explicitly.

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3