Truthy Falsy values

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — What are Truthy and Falsy Values?

In JavaScript, every value has an inherent boolean nature. When used in a boolean context (like an if statement), JavaScript automatically converts values to either true or false.

- A truthy value is a value that converts to true in a boolean context - A falsy value is a value that converts to false in a boolean context

This means you don't always need to use comparison operators in conditionals! Let's see this in action:

let codemonName = "Aquachu"; // Instead of: if (codemonName !== "") if (codemonName) { console.log("You have a Codémon!"); }let codemonLevel = 0; // This won't print because 0 is falsy if (codemonLevel) { console.log("Level up!"); } console.log("Level is:", codemonLevel);

Why is this useful?

Understanding truthy and falsy values makes your code cleaner and more concise. Instead of writing if (value !== null && value !== undefined && value !== ""), you can often just write if (value)!

Exercise 2 — The 6 Falsy Values

In JavaScript, there are exactly 6 falsy values. Everything else is truthy! Here they are:

1. false - the boolean false 2. 0 - the number zero 3. "" - empty string 4. null - intentional absence of value 5. undefined - variable declared but not assigned 6. NaN - Not a Number (result of invalid math operations)

Let's test each one:

console.log("false:", Boolean(false)); console.log("0:", Boolean(0)); console.log("empty string:", Boolean("")); console.log("null:", Boolean(null)); console.log("undefined:", Boolean(undefined)); console.log("NaN:", Boolean(NaN));

Memorize these 6!

These 6 falsy values are crucial to remember. A common mistake is thinking that other values like empty arrays [] or empty objects {} are falsy - but they're NOT! Only these 6 are falsy.

Exercise 3 — Identifying Falsy Values

Which of the following is NOT a falsy value in JavaScript?

Options
• "0"
• 0
• null
• undefined

Hint : Remember: Only 6 values are falsy. The string "0" contains a character, so it's not an empty string.

Exercise 4 — Everything Else is Truthy!

Now for the good news: everything that isn't one of those 6 falsy values is truthy!

This includes some values that might surprise you:

- "0" - string with zero (not empty!) - "false" - string with the word false - -1 or any non-zero number - " " - string with a space - [] - empty array - {} - empty object

console.log("String \"0\":", Boolean("0")); console.log("String \"false\":", Boolean("false")); console.log("-1:", Boolean(-1)); console.log("Space string:", Boolean(" ")); console.log("42:", Boolean(42));

Watch out for strings!

A very common mistake: "0" and "false" are truthy because they are non-empty strings. Only the empty string "" is falsy. Any string with content, even if it says "false", is truthy!

Exercise 5 — Which one is Falsy?

Which of these values will return false when converted to Boolean?

Options
• "0"
• " "
• 0
• []

Hint : Remember the 6 falsy values! Non-empty strings are truthy, even if they contain "0" or a space.

Exercise 6 — Which is Truthy?

Which of these values is truthy?

Options
• "false"
• 0
• null
• ""

Hint : Remember: "false" is a non-empty string, so it's truthy! Only the 6 specific falsy values are falsy.

Exercise 7 — Match Values to Their Nature

Match each value with whether it's truthy or falsy:

Items to match
• ""
• "false"
• 0
• -1
• null
• " "
Definitions
• Falsy
• Truthy
• Falsy
• Truthy
• Falsy
• Truthy

Exercise 8 — Negating Truthy and Falsy

What will this code output?

let codemonHP = 0; console.log(!codemonHP);
Options
• true
• false
• 0
• undefined

Hint : 0 is falsy. The ! operator inverts it, so !0 becomes true!

Exercise 9 — Combining Truthy and Falsy

What will this code output?

let name = "Aquachu"; let hp = 0; console.log(name && hp);
Options
• true
• false
• 0
• "Aquachu"

Hint : && checks if both sides are truthy. name is truthy, but hp is 0 (falsy). What does true && false give?

Exercise 10 — Truthy/Falsy Practice

Practice identifying truthy and falsy values! Create these variables: - emptyName with an empty string - zeroDamage with 0 - type with "fire" Then check if each one is truthy or falsy by printing them with Boolean().

Starter code
// Create your variables and tests here
Back to the course