Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 1.2 KB

falsy_values.md

File metadata and controls

34 lines (28 loc) · 1.2 KB

Falsy values In JavaScript, falsy values are values that are considered false when evaluated in a boolean context. The following values are falsy in JavaScript:

  1. false: The boolean value false.
  2. 0: The number zero.
  3. '' (empty string): An empty string.
  4. null: A special keyword denoting a null value.
  5. undefined: A special keyword denoting an undefined value.
  6. NaN: Not-a-Number, a special value representing an invalid numeric operation.

Here are some ways to tackle falsy values:

  1. Use ! Negation to check for all falsy values If value is false, 0, '', null, undefined, or NaN, the condition is true, and the code inside the if block will execute. For example:

    if (!value) {
      // Do something
    }
  2. Use Ternary or logical OR operators to provide default values for falsy values

    const result = (value) ? value : defaultValue;
    const result = value || defaultValue;
  3. Check for null and undefined Explicitly: If you want to check specifically for null or undefined, use strict equality:

    if (value === null || value === undefined) {
      // Handle null or undefined
    }