On this page

JavaScript Truhy and Falsy Values

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

In JavaScript logic, every single value has an inherent boolean identity. When a value is placed inside a conditional statement (like an if block), JavaScript evaluates it as either true or false. Understanding Truthy and Falsy values is a secret weapon for writing concise, professional code.

What are Falsy Values?​

In JavaScript, there are only a handful of values that evaluate to false when converted to a boolean. These are known as Falsy values. If a value is not on this list, it is automatically considered Truthy.

The "Falsy Eight" list:

  1. false – The boolean false itself.
  2. 0 – The number zero.
  3. 0 – Negative zero.
  4. 0n – BigInt zero.
  5. "" – An empty string (no characters, no spaces).
  6. null – The intentional absence of value.
  7. undefined – An uninitialized variable.
  8. NaN – "Not a Number" (result of a failed math operation).

What are Truthy Values?​

A Truthy value is simply anything that is not in the Falsy list above. This can sometimes be counter-intuitive for beginners.

Common Truthy "Traps":

Logical Context: How JS Uses These Values​

You don't need to manually convert values to booleans; JavaScript does this automatically in boolean contexts, such as if statements or logical operators.

Code Example: Short-Circuit Logic


let username = ""; // This is Falsy



if (username) {

  console.log("Welcome, " + username);

} else {

  console.log("Guest Login"); // This will run because "" is falsy

}



let score = 0; // This is Falsy

if (score >= 0) {

  console.log("Valid Score"); // This runs because we used a comparison operator

}

The Boolean() Function​

If you ever want to explicitly check if a value is Truthy or Falsy, you can use the built-in Boolean() function or the "Double Bang" (!!) operator.

Code Example:


console.log(Boolean("Hello")); // true

console.log(Boolean("")); // false

console.log(!![]); // true (Empty arrays are truthy!)

Summary Table: Truthy vs. Falsy Comparison​

Value TypeFalsy ExampleTruthy Example****Strings"" (Empty)" " (Space), "Hi"Numbers0, NaN``1, -5, 0.1ObjectsNone{}, [] (Always Truthy)Specialsnull, undefined``Infinity


Practical Application: Developers often use Truthy/Falsy to check if data exists. For example, if (userProfile) { ... } is a quick way to see if a profile object is loaded or if it is still null. Mastering this prevents "Cannot read property of undefined" errors!

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Loops

Next

Functions