Falsy
Falsy, as opposed to Truthy, is any value that's considered to be false
when referred to in a Boolean context. Any value is considered to be falsy if it's equal to: false
, 0
, ""
, null
, undefined
, or NaN
.
Real-World Example 🌎
Let's imagine that you're building a web app that has customers and admins, both with different permissions within the application. Determining whether a value is falsy can be helpful when deciding which users can do what:
// here we have a customer object
// that includes details about their profile -
// notably the 'isAdmin' key
var customer_824375 = {
customerId: 824375,
companyName: "SpaceX",
contact: {
firstName: "Elon",
lastName: "Musk",
phone: "800-555-9595",
emailAddress: "elon@spacex.com"
},
isAdmin: false,
subscriptionIsActive: true,
subscriptionPlan: {
type: "Enterprise",
contractTermInMonths: 36,
monthlyCost: 9995
},
isSubscribedToMailingList: null
}
//...
// a section of our app can contain the logic
// that handles sending auto-response emails
// once a support ticket is created by a customer
// generate a *fake* newsletter email
class Email {
constructor(userObj) {
this.user = userObj
var { emailAddress } = this.user.contact
this.send = (emailSubject, emailContent) => {
alert(
`
to: ${emailAddress}\n
from: team@codesnippet.io\n
subject: ${emailSubject}\n
body: ${emailContent}
`
)
}
}
}
var sendNewsletterEmail = (userObj, emailSubject, emailContent) => {
if (userObj.isSubscribedToMailingList) { // <-- currently 'null' which is considered falsy
var newsletter = new Email(userObj) // <-- this code
newsletter.send(emailSubject, emailContent) <-- will not run
}
}
sendNewsletterEmail(customer_824375, "Check out this awesome API tutorial!", "Here's a tutorial on how to use the WikiPedia API in minutes! https://codesnippet.io/wikipedia-api-tutorial/")
//...
// in another area of our application we'd create
// the feature that determines whether or not a user
// has the right permissions to access a particular area of the app
var verifyUsersAdminCredentials = (userObj) => {
if (userObj.isAdmin) { // <-- currently set to 'false' which is falsy
return displayAdminDashboard() // <-- will not run
} else {
return "Please login as an administrator to view this page"
}
}
var displayAdminDashboard = () => {
alert('Logging in to the admin dashboard...')
}
verifyUsersAdminCredentials(customer_824375) // returns "Please login as an administrator to view this page"
Test this code in your browser console
By passing in customer_824375
to verifyUsersAdminCredentials(customer_824375)
like so, we can see when we get to this line: if(userObj.isAdmin)...
, it returns false
and thus, denies the user access to the admin dashboard.
Your turn 🔺
Mess around with the customer object until you can generate the 'email' 📬. Afterwards, see if you can create an object that represents an administrator for the web app, supplying them with the correct permissions. Then try passing the admin object into the verifyUsersAdminCredentials(userObj)
function!
If all goes well, you won't get an email, but you'll get this psuedo-email looking thing:
And you won't see an admin dashboard (because we didn't build that part), but you will see a sweet popup like this:
Wrapping Up ✅
Being able to determine if a value is truthy or falsy is an extremely useful technique in programming. Essentially a falsy value is something that doesn't quite have a value. Just remember, a value is falsy if it's equal to false
, 0
, ""
, null
, undefined
, or NaN
.
Stay Tuned 📺
If you have any questions or improvements, or if you'd like to see additional examples, feel free to reach out to me anytime at tim@timwheeler.com
. If you're enjoying my posts, please click here 📬 or subscribe below 👇!