Truthy

Truthy is any value that's considered to be true when referred to in a Boolean context.

Truthy

Truthy is any value that's considered to be true when referred to in a Boolean context. Essentially any value is deemed to be truthy so long as it's not false, 0, "", null, undefined, or NaN.

Real-World Example 🌎

Let's imagine that a credit card company needed to determine whether or not a customer had any reward points in their account. If so, then the customer is able to redeem their points so long as they have a minimum amount of them. Using truthy values can help us with this:

var client_4591 = {
    id: 4591,
    firstName: 'Benjamin',
    lastName: 'Franklin',
    accountIsActive: true,
    creditCardBalance: 650,
    creditCardLimit: 10000,
    rewardsPointsBalance: 36500,
    accountIsPastDue: false 
}

function redeemRewardsPoints(client, amountOfRewardsPointsToRedeem) {

    // destructure the client object
    const { id, accountIsActive, rewardsPointsBalance, accountIsPastDue } = client
    
    // check if the client's account is active, that their account is not past due
    // and that they're not redeeming more rewards points than they have available
    
    if (rewardsPointsBalance) {
    
        if (accountIsActive && !accountIsPastDue && amountOfRewardsPointsToRedeem <= rewardsPointsBalance) { 

            // update the client's rewards points balance accordingly
            rewardsPointsBalance -= amountOfRewardsPointsToRedeem

            // invoke the confirmation message
            transferRewardsPointsToClient(amountOfRewardsPointsToRedeem, rewardsPointsBalance)

        } else {

            alert('An error occured! Please try again at another time.')

        }
    
    } else {
    
        alert('Unfortunately you do not have any available rewards points to redeem at this time. Please check back later.')
        
    }

}

function transferRewardsPointsToClient(amountOfPointsRedeemed, updatedRewardsPointsBalance){
	
	alert(`Congrats! You've successfully redeemed ${amountOfPointsRedeemed} rewards points! You will be mailed your selected gift card within 10 business days. You have ${updatedRewardsPointsBalance} rewards points remaining.`)

}

redeemRewardsPoints(client_4591, 500)

Test this code in your browser console

You can see above that if(rewardsPointsBalance) ... is not explicitly checking for a Boolean value, however the compiler refers to rewardsPointsBalance in a Boolean context. Thus, the function continues to execute so long as rewardsPointsBalance is not deemed to be falsy. In this case, rewardsPointsBalance is an integer, so if we had set it's value to 0, which is falsy, the function would've skipped over the if-statement and executed the else-statement.

The same also goes for accountIsActive. In this case, it's value is true, but there's no need to explicitly check whether or not the value is true by writing out this: if(accountIsActive == true).... Checking if(accountIsActive)... is enough to satisfy the compiler, telling it whether or not the account is indeed active.

Wrapping Up ✅

Checking if a value is truthy is simple a matter of making sure the value in question is not 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 [email protected]. If you're enjoying my posts, please subscribe below 👇 or click here 📬!

Help us improve our content