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
Code Example
PREVIEW READY
Loading Editor...
Live Preview

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
Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ Practical Use in if Conditions

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

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

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - Optional Chaining