Course

Truthy/Falsy Values

Loading...

In this lesson, you'll learn about truthy and falsy values in JavaScript, which are crucial for understanding how conditions are evaluated in your code.

Truthy and Falsy Values

In the previous section, we learned about strict and loose equality in JavaScript. Equality always results in a boolean value, which can either be or .

Unlike some other languages, and in JavaScript are not limited to boolean data types and comparisons . They can take many other forms.

In JavaScript, we have something known as truthy values and falsy values.

  • Truthy expressions always evaluate to boolean .
  • Falsy expressions always evaluate to boolean .

Understanding truthy and falsy values in JavaScript is essential . If you don't know which values evaluate to truthy and which to falsy, you might struggle to read and understand other people's code.

Longtime JavaScript developers often use the terms "truthy" and "falsy" but for those new to JavaScript, these terms can be a bit confusing.

When we say a value is "truthy" in JavaScript, we don't just mean that the value is . Rather, it means the value coerces to when evaluated in a boolean context. Let's explore what that means.

Falsy Values

The easiest way to learn truthy and falsy values is to memorize the falsy values. There are only six falsy values; all other values are truthy.

FALSY VALUES:

  • (zero)
  • , , (empty strings)
  • (not a number)

Note: An empty array is not falsy.

Truthy Values

Everything that is not falsy

That's a pretty straightforward list. But how can we actually use truthiness? Let's look at an example.

Using Truthiness in Code

Taking advantage of truthiness can make your code more concise. You don't need to explicitly check for , , etc. Instead, you can just check whether a value is truthy. However, there are some caveats to keep in mind.

Example:

let value = "Hello";

if (value) {
    console.log("This is a truthy value!");
} else {
    console.log("This is a falsy value!");
}
Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Logical Operators Part 2 (&&, ||)