Truthy and Falsy values in Javascript

In JavaScript, every value is either truthy or falsy when evaluated in a boolean context (such as in if statements or logical operations). This is important to understand when dealing with conditional statements or logical operators. Here’s a breakdown of truthy and falsy values.

1. Falsy Values

A falsy value is a value that is considered false when encountered in a boolean context. There are only a few falsy values in JavaScript:

List of Falsy Values:

  1. false — the boolean false.
  2. 0 — the number zero.
  3. -0 — negative zero.
  4. 0n — BigInt zero (0n).
  5. "" (empty string) — a string with no characters.
  6. null — represents no value.
  7. undefined — a variable that has not been assigned a value.
  8. NaN — “Not-a-Number,” a result of invalid mathematical operations.

Example:

if (false) {
  console.log("This won't run");
}

if (0) {
  console.log("This won't run");
}

if (undefined) {
  console.log("This won't run");
}

if ("") {
  console.log("This won't run because the string is empty");
}

2. Truthy Values

A truthy value is any value that is not falsy. In other words, any value that is not on the list of falsy values will be considered truthy when evaluated in a boolean context. This includes:

  • Non-zero numbers (both positive and negative).
  • Non-empty strings.
  • Objects (including empty objects {} and arrays []).
  • Any other value not listed in the “falsy” list above.

Example:

if (true) {
  console.log("This will run because true is truthy");
}

if (42) {
  console.log("This will run because 42 is truthy");
}

if ("hello") {
  console.log("This will run because non-empty strings are truthy");
}

if ([]) {
  console.log("This will run because an empty array is truthy");
}

if ({}) {
  console.log("This will run because an empty object is truthy");
}

Common Falsy Values in Action

Example of handling falsy values:

let value = 0;

if (!value) {
  console.log("This runs because 0 is falsy"); // This line will run
}

let emptyString = "";

if (!emptyString) {
  console.log("This runs because an empty string is falsy"); // This line will run
}

Truthiness in Logical Operations

In JavaScript, truthy and falsy values play an important role in logical operations, particularly with the logical AND (&&) and OR (||) operators:

  • Logical AND (&&): Evaluates to the first falsy value or the last truthy value if all are truthy.
  • Logical OR (||): Evaluates to the first truthy value or the last falsy value if none are truthy.

Example:

let result = "" || "default";
console.log(result); // Output: "default" (because "" is falsy)

let num = 0 && 42;
console.log(num); // Output: 0 (because 0 is falsy)

let truthyValue = "hello" && "world";
console.log(truthyValue); // Output: "world" (because both are truthy)

Summary of Truthy and Falsy

Falsy Values:

  • false
  • 0
  • -0
  • 0n
  • "" (empty string)
  • null
  • undefined
  • NaN

Truthy Values:

  • Any value that is not falsy (e.g., true, 1, "non-empty string", {}, [], etc.).

Understanding truthy and falsy values is essential for writing clean, concise conditional logic in JavaScript. It enables you to use shorter syntax for default values, conditional checks, and handling edge cases efficiently.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *