Last modified: May 13, 2026 By Alexander Williams
Check if Variable is Undefined in JS
Undefined is a common value in JavaScript. It means a variable has been declared but not assigned any value. This often causes bugs in your code. You need to check for undefined safely.
There are several ways to check if a variable is undefined. The best method depends on the situation. We will cover the most reliable techniques.
Why Checking for Undefined Matters
If you try to use an undefined variable, your script may crash. This is bad for user experience. Checking before use prevents errors. It also makes your code more robust.
Beginners often confuse undefined with null or an empty string. They are different. Undefined means the variable has no value at all. Null is an intentional empty value. Knowing the difference helps you write cleaner code.
Method 1: Using the typeof Operator
The safest way to check for undefined is with the typeof operator. It does not throw an error if the variable is not declared. This makes it ideal for beginners.
// Example: Check if a variable is undefined using typeof
let userName;
if (typeof userName === 'undefined') {
console.log('userName is undefined');
} else {
console.log('userName has a value');
}
Output:
userName is undefined
typeof returns a string. It always returns "undefined" for undefined variables. It also works for undeclared variables without causing an error.
// typeof works even if the variable is not declared
if (typeof someRandomVar === 'undefined') {
console.log('Variable does not exist');
}
// No error thrown
Method 2: Strict Equality (===) with undefined
You can also compare directly to the global undefined property. This is simple and readable. However, it only works if the variable is declared.
// Example: Using strict equality
let age;
if (age === undefined) {
console.log('age is undefined');
}
Output:
age is undefined
Important: Do not use loose equality (==). Loose equality treats undefined and null as equal. This can lead to false positives. Always use === for precise checks.
// Avoid loose equality
let value = null;
if (value == undefined) {
console.log('This runs because null == undefined is true');
}
// This is not what you want
Method 3: Using the Logical OR (||) Operator
Sometimes you want to provide a default value when a variable is undefined. The logical OR operator is perfect for this. It returns the right side if the left is falsy.
// Example: Provide a default value
let score;
let finalScore = score || 0;
console.log(finalScore); // Output: 0
Output:
0
Caution: The OR operator treats other falsy values (like 0, "", false) the same as undefined. If those are valid values, use a different method. For example, score = 0 would still become 0, but if you want to keep 0 as a real value, use the nullish coalescing operator instead.
Method 4: Nullish Coalescing Operator (??)
The ?? operator is newer. It returns the right side only if the left is null or undefined. It ignores other falsy values like 0 or empty strings.
// Example: Using nullish coalescing
let count = 0;
let result = count ?? 10;
console.log(result); // Output: 0 (because 0 is not null or undefined)
Output:
0
This is often better than || when you need to preserve falsy values. Use it when you only want to replace undefined or null.
How to Check Multiple Variables
Sometimes you need to check if several variables are undefined. You can combine checks with logical operators. This keeps your code clean.
// Check multiple variables
let a, b = 5, c;
if (a === undefined || c === undefined) {
console.log('At least one variable is undefined');
}
Output:
At least one variable is undefined
Common Pitfalls to Avoid
One common mistake is using typeof without comparing to a string. Always write typeof x === 'undefined'. Another mistake is forgetting to declare variables with let, const, or var. Undeclared variables cause errors in strict mode.
Also, avoid reassigning the global undefined variable. In modern JavaScript, undefined is read-only. But in older code, it could be changed. Using typeof avoids this risk entirely.
If you want to learn more about declaring variables correctly, read our JavaScript Variable Declaration Guide. It covers best practices for let, const, and var.
Best Practice Summary
For most cases, use typeof when you are unsure if the variable exists. Use === undefined when you know the variable is declared. Use ?? for default values that should only replace undefined or null.
Always test your code. Use console logs or a debugger. This helps you catch undefined variables early.
Conclusion
Checking if a variable is undefined is a fundamental skill in JavaScript. The typeof operator is the safest and most reliable method. It works for both declared and undeclared variables. Strict equality with === undefined is fine for declared variables. The nullish coalescing operator ?? is great for defaults.
Remember to avoid loose equality and other pitfalls. Practice these methods in your projects. They will save you from many bugs. For a deeper understanding of variable types, see our JavaScript Variable Types Guide. It explains the difference between undefined, null, and other types.
Finally, review our JavaScript Variables Guide for a complete overview. Happy coding