Tip 17: Shorten Conditionals with Falsy Values

In this tip, you’ll learn how to use falsy and truthy values to check for information from different types.

Conditionals

Can you remember the first line of code you ever wrote? I can’t, but I wouldn’t be surprised if it was some sort of conditional. Responding one way to some information and a different way to other information is about as basic as programming can get.

I still write a lot of conditionals every day, and I bet you do, too. Fortunately, JavaScript, along with many other languages, gives you many tools for checking information and reassigning or standardizing information very quickly with minimal code.

The secret to being able to check values quickly is to understand the subtle difference between the primitive values true and false (also called Boolean types)and the many so-called truthy and falsy values—values that aren’t identical to the Boolean values true or false but act like they are in most cases.

Equivalency & identity

Give me a moment to review another concept: equivalency and identitya value that’s equivalent if it’s the same, but of a different type and is checked with ‘==.

Get hands-on with 1400+ tech skills courses.