Falsy Values and Truthy Values

Modified on Mon, 1 Jul at 2:34 PM

In JavaScript, values can be categorized as either "truthy" or "falsy" when evaluated in a Boolean context. 


A Boolean context is any situation where a value is interpreted as `true` or `false`. Examples of Boolean contexts include conditional statements (e.g., if, while), logical operators (e.g., &&, ||), and ternary operators.


A “falsy” value is interpreted as `false`, while a “truthy” value is anything that is not falsy.


Falsy Values

A falsy value is a value that is considered false when encountered in a Boolean context. JavaScript has a fixed set of falsy values:

  • 0

  • -0

  • null

  • undefined

  • NaN

  • "" / '' / `` (empty string)

  • false


These values are important because they allow for shorthand conditional checks. For example, to check if a string `str` is empty, you can simply write `!str`as condition instead of `str.length === 0`.

let str = "";

if (!str) {

  console.log("String is empty");

}


Truthy Values

A truthy value is any value that is not falsy. This includes all other values not in the falsy list, such as:

  • All other numbers (including negative numbers and fractions)

  • Non-empty strings

  • Objects

  • Arrays

  • Functions

  • The boolean true


Truthy values help developers write more efficient and readable conditionals. For example, to check if an input value `num` is 0 or not, you can simply write:


let num = 0;

if (num) {

  console.log("Input number is non zero.");

} else {

  console.log(“Input number is zero.”);

}


Common Pitfalls

Edge Cases and Common Mistakes

It's important to be aware of potential pitfalls when using falsy and truthy values. 


  1. In JavaScript, `NaN` is not equal to itself:


console.log(NaN === NaN);  // false


This can be surprising and may cause issues if you expect `NaN` to behave like other falsy values. For example, when comparing other falsy values with themselves using `===`, they return `true`:


console.log(0 === 0);  // true

console.log(-0 === -0);  // true

console.log(null === null);  // true

console.log(undefined === undefined);  // true

console.log("" === "");  // true

console.log(false === false);  // true


However, `NaN` does not behave this way:

console.log(NaN === NaN);  // false


Instead of comparing with `NaN`, you should check if a value is falsy directly:


let value = NaN;

if (!value) {

  console.log("Value is falsy");

}


  1. An empty array (`[]`) or an empty object (`{}`) are truthy


if ([]) {

  console.log("Empty array is truthy");  // This will be logged

}

if ({}) {

  console.log("Empty object is truthy");  // This will be logged

}

Here are some problem where this concept is used:


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article