DEV Community

Anthony Bañon Arias
Anthony Bañon Arias

Posted on

Checking `null`, `undefined`, Empty, and Falsy Values in JavaScript

🔹 Introduction

If you have written JavaScript for a while, you’ve surely faced one of the most common challenges:

👉 distinguishing between null, undefined, empty strings, zero, or other "falsy" values.

It can be confusing, since JavaScript has its own truthy/falsy evaluation system. Sometimes you only need to check whether a variable exists, but in other cases you must know whether it is specifically null, undefined, "", or even an empty object or array.

This article provides a comprehensive cheatsheet to cover all these scenarios, with explanations, code snippets, and practical use cases. Save it as your personal reference when debugging or writing clean conditions in JavaScript.


🔹 General Concept: Truthy vs Falsy

In JavaScript, values are evaluated in Boolean contexts as either truthy or falsy.

✅ Falsy values (evaluate to false):

  • false
  • 0, -0, 0n (BigInt zero)
  • "" (empty string)
  • null
  • undefined
  • NaN

Everything else is considered truthy (including "0", "false", [], {}, etc.).

Example:

if (!0) console.log("0 is falsy");       // ✅
if (!"") console.log("Empty string");    // ✅
if (!null) console.log("Null is falsy"); // ✅
Enter fullscreen mode Exit fullscreen mode

Cheatsheet: Common Checks

Check Code What it validates
Falsy in general if (!x) null, undefined, 0, "", NaN, false
Is defined if (x !== undefined) Ensures x is not undefined
Is null if (x === null) Only null
Is null or undefined if (x == null) True for both null and undefined
Is NOT null/undefined if (x != null) Means x has a real value
Empty string if (x === "") String with length 0
Non-empty string if (typeof x === "string" && x.trim() !== "") Ensures it’s a meaningful string
Valid number if (!isNaN(x)) Not NaN
Empty array if (Array.isArray(x) && x.length === 0) Array with no elements
Non-empty array if (Array.isArray(x) && x.length > 0) Array with elements
Empty object if (x && Object.keys(x).length === 0) Object without properties
Non-empty object if (x && Object.keys(x).length > 0) Object with properties

Use Cases and Examples

1. Checking if a variable has any value

let user;
if (user == null) {
  console.log("User is either null or undefined");
}

Enter fullscreen mode Exit fullscreen mode

2. Avoiding errors with optional values

let config = {};
if (config.apiKey != null) {
  console.log("API Key is set:", config.apiKey);
}
Enter fullscreen mode Exit fullscreen mode

3. Detecting empty input

let input = "";
if (input === "") {
  console.log("Please provide a value");
}
Enter fullscreen mode Exit fullscreen mode

4. Validating arrays

let items = [];
if (Array.isArray(items) && items.length === 0) {
  console.log("The list is empty");
}

Enter fullscreen mode Exit fullscreen mode

5. Ensuring an object has properties

let settings = {};
if (settings && Object.keys(settings).length === 0) {
  console.log("Settings object is empty");
}
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Use == null when you want to catch both null and undefined.
if (value == null) console.log("Null or undefined");
Enter fullscreen mode Exit fullscreen mode
  1. Use === null or === undefined when you want strict checks.

  2. Always check type + content for strings and arrays (typeof x === "string" && x.trim() !== "").

  3. Use Array.isArray() instead of instanceof Array for reliability.

  4. For objects, combine truthiness and Object.keys(obj).length to ensure they are not empty.


Conclusion

Dealing with null, undefined, empty strings, or falsy values is part of everyday JavaScript coding. The key is knowing when to use general falsy checks (if (!x)) versus specific conditions (x === null, Array.isArray(x), etc.).

With this cheatsheet, you’ll avoid common pitfalls and write cleaner, more robust conditions. Bookmark this article or keep it handy as a quick reference for your next JavaScript project. 🚀


🔗 References:

Top comments (0)