Truthy and Falsy in JavaScript

In JavaScript, values can be evaluated in a boolean context to determine if they are "truthy" or "falsy". This evaluation is essential when using conditional statements like if, while, and logical operators. Here’s a detailed explanation:

Falsy Values

A falsy value is a value that translates to false when evaluated in a boolean context. In JavaScript, the following values are considered falsy:

  1. false: The boolean value false.

  2. 0: The number zero.

  3. -0: Negative zero (rarely used, but still falsy).

  4. 0n: The BigInt representation of zero.

  5. "": An empty string.

  6. null: The absence of any value.

  7. undefined: A declared variable without an assigned value.

  8. NaN: The result of a mathematical operation that is not a number.

Truthy Values

A truthy value is any value that is not falsy. In other words, all values except the ones listed above as falsy are truthy. Here are some common examples of truthy values:

  1. true: The boolean value true.

  2. Non-zero numbers: Any number other than 0 or -0, including negative numbers.

  3. Non-empty strings: Any string with at least one character, including whitespace (e.g., "0", "false", " ", "hello").

  4. Objects: All objects are truthy, including empty objects ({}) and arrays ([]).

  5. Symbol: Any value of type Symbol.

  6. Functions: All functions are truthy.

Examples

Falsy Values

if (!false) {
  console.log('This is falsy');  // Output: This is falsy
}

if (!0) {
  console.log('This is falsy');  // Output: This is falsy
}

if (!"") {
  console.log('This is falsy');  // Output: This is falsy
}

if (!null) {
  console.log('This is falsy');  // Output: This is falsy
}

if (!undefined) {
  console.log('This is falsy');  // Output: This is falsy
}

if (!NaN) {
  console.log('This is falsy');  // Output: This is falsy
}

Truthy Values

if (true) {
  console.log('This is truthy');  // Output: This is truthy
}

if (42) {
  console.log('This is truthy');  // Output: This is truthy
}

if ("hello") {
  console.log('This is truthy');  // Output: This is truthy
}

if ({}) {
  console.log('This is truthy');  // Output: This is truthy
}

if ([]) {
  console.log('This is truthy');  // Output: This is truthy
}

if (function() {}) {
  console.log('This is truthy');  // Output: This is truthy
}

Practical Usage

Conditional Statements

Truthy and falsy values are commonly used in conditional statements to control the flow of the program:

let value = "hello";

if (value) {
  console.log('Value is truthy');  // Output: Value is truthy
} else {
  console.log('Value is falsy');
}

Default Values

You can use truthy and falsy evaluations to set default values:

function greet(name) {
  name = name || 'Guest';  // If name is falsy, default to 'Guest'
  console.log('Hello, ' + name);
}

greet('Alice');  // Output: Hello, Alice
greet();         // Output: Hello, Guest

In the example above, if name is undefined or another falsy value, it defaults to 'Guest'.

Understanding truthy and falsy values is crucial for writing efficient and bug-free JavaScript code, especially when dealing with conditional logic and default values.