Truthy and Falsy

Truthy Falsy
πŸ‘¨β€πŸ’Ό We often need to decide if a value "exists" without comparing it to null or undefined. JavaScript treats some values as falsy, and everything else as truthy.
Falsy values:
  • false
  • 0, -0, 0n
  • '' (empty string)
  • null
  • undefined
  • NaN
You can convert any value to a boolean with Boolean(value) or !!value.
🐨 Open
index.ts
. The starter provides these fixtures:
NameValue
username'kent'
nickname'' (empty string)
age0
email'kent@epicweb.dev'
password'' (empty string)
notesundefined
cartTotal42
hasAcceptedTermsfalse
Create and export:
  1. hasUsername β€” boolean for whether username is truthy
  2. hasNickname β€” boolean for whether nickname is truthy
  3. hasAge β€” boolean for whether age is truthy
  4. hasNotes β€” boolean for whether notes is truthy
  5. canCheckout β€” true only when cartTotal is truthy and hasAcceptedTerms is true
  6. canCreateAccount β€” true only when hasUsername, email, and password are all truthy

Required exports

hasUsername, hasNickname, hasAge, hasNotes, canCheckout, canCreateAccount

Completion criteria

Each export must be a real boolean (typeof "boolean"). Use the fixtures and the falsy list above to reason about (or log) what each expression evaluates toβ€”don't hardcode the answers.
πŸ’° Convert values to booleans when you need a strict true/false.
πŸ“œ MDN - Truthy πŸ“œ MDN - Falsy

Please set the playground first

Loading "Truthy and Falsy"
Loading "Truthy and Falsy"