🔹 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"); // ✅
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");
}
2. Avoiding errors with optional values
let config = {};
if (config.apiKey != null) {
console.log("API Key is set:", config.apiKey);
}
3. Detecting empty input
let input = "";
if (input === "") {
console.log("Please provide a value");
}
4. Validating arrays
let items = [];
if (Array.isArray(items) && items.length === 0) {
console.log("The list is empty");
}
5. Ensuring an object has properties
let settings = {};
if (settings && Object.keys(settings).length === 0) {
console.log("Settings object is empty");
}
Best Practices
- Use
== null
when you want to catch both null and undefined.
if (value == null) console.log("Null or undefined");
Use
=== null
or=== undefined
when you want strict checks.Always check type + content for strings and arrays
(typeof x === "string" && x.trim() !== "")
.Use
Array.isArray()
instead ofinstanceof
Array for reliability.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. 🚀
Top comments (0)