Javascript Truthy And Falsy Values Complete Guide

 Last Update:2025-06-22T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    8 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of JavaScript Truthy and Falsy Values

JavaScript Truthy and Falsy Values

Truthy Values: In JavaScript, a Truthy value is a value that translates to true when evaluated in a Boolean context. Essentially, any value not included in the list of Falsy values is considered Truthy. These are the most common Truthy values:

  • Any String: Even an empty string technically contains no characters, but in logical contexts, all non-empty strings are treated as True.

    • Example: "hello", " ", "false"
  • Any Number Except Zero: All numbers except zero (0), Infinity, -Infinity, NaN (Not-a-Number) are considered True.

    • Example: 1, -5, 3.14
  • Objects: All objects are truthy, even if they are empty.

    • Example: {}, { name: "Alice" }
  • Arrays: Every array evaluates to True, including empty arrays.

    • Example: [], [1, 2, 3]
  • Functions: Any function will be truthy.

    • Example: function add() { return 5 + 7; }, const myFunc = () => {}
  • Symbols: Introduced in ES6, Symbols are used as unique identifiers and are considered truthy.

    • Example: Symbol('description')

Falsy Values: On the contrary, there are only six specific Falsy values in JavaScript. A Falsy value is a value that translates to false when evaluated in a Boolean context:

  1. null - Represents the intentional absence of any object value.

    • Example: let x = null;
  2. undefined - Denotes a variable has not been assigned a value, thus it is undefined.

    • Example: let y; console.log(y); // prints undefined
  3. false - The Boolean primitive value representing logical falsity.

    • Example: let bool = false;
  4. 0 - The number zero is considered falsy because it denotes emptiness or no quantity.

    • Example: let num = 0;
  5. NaN - Stands for “Not a Number.” Typically occurs as a result of a failed operation that was intended to return a number.

    • Example: let invalidNum = 'a' * 1; // NaN
  6. "" (Empty String) - An empty string is considered falsy, signifying it contains no characters.

    • Example: let str = "";

Evaluating Conditions Using Boolean Context

When conditions are evaluated in JavaScript, the truthiness or falsiness of the value is what dictates the flow of the program. Here are some practical examples demonstrating evaluations:

if (true) {
    console.log("This executes");
}

if ("hello") {
    console.log("Strings are truthy except the empty string");
}

if (42) {
    console.log("Numbers (except 0, Infinity, etc.) are also truthy");
}

if ([]) {
    console.log("Even empty arrays evaluate to true");
}

if ({}) {
    console.log("Empty objects are considered true as well");
}

if (!false) {
    console.log("Negating a falsy value makes it truthy");
}

if (!null) {
    console.log("!null => true");
}

if (!undefined) {
    console.log("!undefined => true");
}

if (!0) {
    console.log("!0 => true");
}

if (!NaN) {
    console.log("!NaN => true");
}

if (!"") {
    console.log("!'' => true");
}

Practical Implications and Tips

  • Using !! to Convert to Boolean: You can double-negate a value using !! to explicitly convert it to true or false. This technique comes handy when debugging or ensuring the type of returned values.

    • Example: console.log(!!""); // false
    • Example: console.log(!!0); // false
    • Example: console.log(!!{}); // true
    • Example: console.log(!!true); // true
  • Short-Circuit Behavior: Logical operators (&& and ||) in JavaScript exhibit short-circuit behavior. This means that in an && operation, the second operand is evaluated only if the first is truthy. Similarly, in an || operation, the second operand is evaluated only if the first is falsy.

    • Example (&&): console.log(true && "hello"); // "hello"
    • Example (&&): console.log(false && "hello"); // false
    • Example (||): console.log(false || "hello"); // "hello"
    • Example (||): console.log(true || "hello"); // true
  • Nullish Coalescing Operator ??: Introduced in ES2020, this operator provides a way to select the first non-nullish value from two operands. In the context of JavaScript, nullish values are null and undefined.

    • Example: let foo = null ?? 'Default'; // Default
    • Example: let bar = undefined ?? 'Default'; // Default
    • Example: let baz = '' ?? 'Default'; // ''
    • Example: let qux = 0 ?? 'Default'; // 0
  • Handling Undefined Variables: Before accessing properties or methods on potentially undefined variables, it’s good practice to check for undefined (or null) to avoid potential runtime errors.

    • Example:
      let obj;
      if (obj) {
          console.log(obj.name);
      } else {
          console.log("obj is undefined, cannot access obj.name");
      }
      

Understanding these concepts will help you write more efficient, bug-free code by leveraging JavaScript's built-in Boolean coercion rules. Always strive to clarify the context and intent of your conditions, and use appropriate checks to handle both Truthy and Falsy values.

General Keywords for SEO

boolean, falsy, truthy, logical operators, short-circuit, double negation, !!, nullish coalescing operator, ?, undefined variables, null, context, coercion, programming, JavaScript, examples, conditions, evaluation, true, false, empty string, zero, NaN, objects, arrays, functions, symbols, debugging, efficient code, runtime errors, JavaScript truth table, conditionals in JavaScript, handling undefined, checking for null, boolean values in JavaScript, understanding truthiness, falsiness in JavaScript, JS truthy and falsy, conditional statements in JS, JavaScript truthy, JavaScript falsy

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement JavaScript Truthy and Falsy Values

What are Truthy and Falsy Values?

In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context (like an if statement). Conversely, a falsy value is one that evaluates to false.

There are only 6 falsy values in JavaScript:

  1. false
  2. 0 (zero)
  3. -0 (negative zero)
  4. "" (empty string)
  5. null
  6. undefined
  7. NaN (Not-a-Number)

All other values are truthy.

Step-by-Step Examples

Example 1: Testing Falsy Values in Conditional Statements

Let's evaluate different types of falsy values in if statements.

// Using false
if (false) {
    console.log("This won't get printed.");
} else {
    console.log("False is falsy.");
}

// Using 0
if (0) {
    console.log("This won't get printed.");
} else {
    console.log("0 is falsy.");
}

// Using -0
if (-0) {
    console.log("This won't get printed.");
} else {
    console.log("-0 is falsy.");
}

// Using empty string ""
if ("") {
    console.log("This won't get printed.");
} else {
    console.log('Empty string "" is falsy.');
}

// Using null
if (null) {
    console.log("This won't get printed.");
} else {
    console.log("Null is falsy.");
}

// Using undefined
if (undefined) {
    console.log("This won't get printed.");
} else {
    console.log("Undefined is falsy.");
}

// Using NaN
if (NaN) {
    console.log("This won't get printed.");
} else {
    console.log("NaN is falsy.");
}

Output:

False is falsy.
0 is falsy.
-0 is falsy.
Empty string "" is falsy.
Null is falsy.
Undefined is falsy.
NaN is falsy.

Example 2: Testing Truthy Values in Conditional Statements

Here, let's write if statements to test some common truthy values.

// Using a non-zero number
if (5) {
    console.log("Five is truthy.");
} else {
    console.log("This won't get printed.");
}

// Using a non-empty string
if ("Hello world!") {
    console.log("A non-empty string is truthy.");
} else {
    console.log("This won't get printed.");
}

// Using an array, even if it's empty
if ([]) {
    console.log("An empty array is truthy.");
} else {
    console.log("This won't get printed.");
}

// Using an object, even if it's empty
if ({}) {
    console.log("An empty object is truthy.");
} else {
    console.log("This won't get printed.");
}

// Using the boolean true
if (true) {
    console.log("True is truthy.");
} else {
    console.log("This won't get printed.");
}

Output:

Five is truthy.
A non-empty string is truthy.
An empty array is truthy.
An empty object is truthy.
True is truthy.

Example 3: Using Double Negation (!!) to Determine Whether a Value is Truthy or Falsy

The double negation (!!) operator returns a Boolean value representing the truthiness of a variable.

// Falsy values
let falseValue = false;
console.log(!!falseValue); // false

let zeroValue = 0;
console.log(!!zeroValue); // false

let negativeZeroValue = -0;
console.log(!!negativeZeroValue); // false

let emptyStringValue = "";
console.log(!!emptyStringValue); // false

let nullValue = null;
console.log(!!nullValue); // false

let undefinedValue;
console.log(!!undefinedValue); // false

let nanValue = NaN;
console.log(!!nanValue); // false

// Truthy values
let fiveValue = 5;
console.log(!!fiveValue); // true

let nonEmptyStringValue = "Hello";
console.log(!!nonEmptyStringValue); // true

let nonEmptyArrayValue = [1, 2];
console.log(!!nonEmptyArrayValue); // true

let nonEmptyObjectValue = { key: "value" };
console.log(!!nonEmptyObjectValue); // true

let trueValue = true;
console.log(!!trueValue); // true

Output:

false
false
false
false
false
false
false
true
true
true
true
true

Example 4: Practical Usage with Functions

You can use truthy and falsy values to check function parameters or default values.

// Function that uses truthy and falsy to check for empty strings
function greet(name) {
    let greeting = name ? `Hello, ${name}!` : 'Hello, stranger!';
    console.log(greeting);
}

greet(""); // Output: Hello, stranger!
greet("Alice"); // Output: Hello, Alice!

// Function using || with default parameters
function multiply(a, b) {
    a = a || 1;
    b = b || 1;
    return a * b;
}

console.log(multiply()); // Output: 1 (both parameters are falsy and set to 1)
console.log(multiply(5)); // Output: 5 (only one parameter is given, so b defaults to 1)
console.log(multiply(5, 2)); // Output: 10 (both parameters are provided)

Example 5: Using Truthy and Falsy for Loop Control

In loops, you often rely on the truthiness of expressions to control when the loop should continue or terminate.

let data = ["apple", "", "banana", null, "cherry"];

for (let item of data) {
    if (item) {
        console.log(item);
    } else {
        console.log("Found a falsy value:", item);
    }
}

Output:

You May Like This Related .NET Topic

Login to post a comment.