Understanding Truthy and Falsy Values in JavaScript

In this lesson, we'll discover truthy and falsy values in JavaScript. These values play a fundamental role in the decision-making structures found in code. Unique to programming, these concepts form the basis of how JavaScript and many other programming languages make decisions.

In JavaScript, a value can be considered 'truthy' or 'falsy'. If a value is interpreted as true in a context where a boolean is expected, it is considered 'truthy'. Conversely, if it is interpreted as false, it is considered 'falsy'. For example, 1 (the number one) is truthy, while 0 (the number zero) is falsy. Most values are considered truthy, but others like false, 0, '' or "", null, undefined, and NaN are inherently falsy.

Conditional Statements in JavaScript

Conditional statements give your code the ability to behave differently under various conditions. An "if-else" statement, for instance, executes one block of code if the condition is truthy and another block if it's falsy. The anatomy of if-else statement is shown below:

In this if statement, the code inside the {} is the block. It only runs if the condition is true, similarly the else branch has its block which runs if the condition is false. In JavaScript, a block refers to a group of code enclosed within curly braces {}. Think of a block like a container that holds related code together. Blocks are important in JavaScript because they define the scope of different code statements, which means they control where certain variables and functions can be accessed and used in your code.

Let's consider an example we all could relate to, an online test score updater:

If the test score is more than 50, 'Congratulations, you passed!' is printed. Otherwise, it prints 'Sorry, you failed. Better luck next time.'.

Equality Operation Examples

To continue this lesson, let's review equality operations. These will assist us in comparing values and expressions. We often use == and ===, which represent equality and strict equality, respectively, as well as <, >, <=, and >= for comparisons. This is akin to comparing the weights of apples to select the heaviest one:

Unraveling Logical Operators

When multiple conditions must be considered simultaneously, JavaScript provides the perfect tools: logical operators. We have AND (&&), OR (||), and NOT (!).

The AND operator (&&) behaves like the word 'and'. It tests whether both of its conditions are true. This is akin to checking if you have apples and if a bakery is open to determine if you can make apple pie:

However, if you're fine with making either an apple pie or a banana smoothie, the OR operator (||) is applicable. It tests whether at least one of the conditions is true. This equates to checking whether you have either apples or bananas at home to determine which dessert to make:

While the AND (&&) and OR (||) operators are great for testing multiple conditions, sometimes you need to check if a condition is not true. This is where the NOT operator (!) comes into play.

The NOT operator is like saying 'not' or 'no' in English. It inverts the truthiness of a condition. For example, you want to check if the bakery is not closed:

Here, if bakeryOpen is false, !bakeryOpen becomes true, and it will print the message indicating that the bakery is closed.

Conclusion

Congratulations! In this lesson, we have familiarized ourselves with truthy and falsy values, conditional statements, logical operators, and equality operations. We've tied these concepts to a real-life situation: deciding what dessert to make based on the fruits available at home.

In the next steps, you'll apply these concepts yourself in the exercise section. Try creating your own coding scenarios. The more practice you get, the more you'll enhance your skills!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal