Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to determine if a variable is 'undefined' or 'null'?
In JavaScript, checking for null and undefined values is a common requirement. There are several approaches to determine if a variable is null or undefined.
Understanding null vs undefined
undefined means a variable has been declared but not assigned a value, while null is an intentional absence of value.
<html>
<body>
<script>
let undefinedVar;
let nullVar = null;
console.log("undefinedVar:", undefinedVar);
console.log("nullVar:", nullVar);
console.log("typeof undefinedVar:", typeof undefinedVar);
console.log("typeof nullVar:", typeof nullVar);
</script>
</body>
</html>
undefinedVar: undefined nullVar: null typeof undefinedVar: undefined typeof nullVar: object
Method 1: Using Strict Equality (===)
The most precise way is to check for each value explicitly using strict equality:
<html>
<body>
<script>
let testVar1;
let testVar2 = null;
let testVar3 = 10;
function checkVariable(variable, name) {
if (variable === undefined) {
console.log(name + " is undefined");
} else if (variable === null) {
console.log(name + " is null");
} else {
console.log(name + " has value: " + variable);
}
}
checkVariable(testVar1, "testVar1");
checkVariable(testVar2, "testVar2");
checkVariable(testVar3, "testVar3");
</script>
</body>
</html>
testVar1 is undefined testVar2 is null testVar3 has value: 10
Method 2: Using Loose Equality (==)
To check for both null and undefined together, you can use loose equality since null == undefined returns true:
<html>
<body>
<script>
let age = 10;
let name;
let address = null;
function isNullOrUndefined(value) {
return value == null;
}
console.log("age is null/undefined:", isNullOrUndefined(age));
console.log("name is null/undefined:", isNullOrUndefined(name));
console.log("address is null/undefined:", isNullOrUndefined(address));
</script>
</body>
</html>
age is null/undefined: false name is null/undefined: true address is null/undefined: true
Method 3: Using Truthiness Check
A simple truthiness check can detect both values, but be aware it also catches other falsy values like 0, "", and false:
<html>
<body>
<script>
let age = 10;
let count = 0;
let name;
function checkTruthiness(variable, varName) {
if (variable) {
console.log(varName + " is truthy: " + variable);
} else {
console.log(varName + " is falsy: " + variable);
}
}
checkTruthiness(age, "age");
checkTruthiness(count, "count");
checkTruthiness(name, "name");
</script>
</body>
</html>
age is truthy: 10 count is falsy: 0 name is falsy: undefined
Comparison
| Method | Detects null | Detects undefined | Other falsy values |
|---|---|---|---|
variable === null |
Yes | No | No |
variable === undefined |
No | Yes | No |
variable == null |
Yes | Yes | No |
!variable |
Yes | Yes | Yes (0, "", false) |
Conclusion
Use variable == null to check for both null and undefined together, or strict equality (===) when you need to distinguish between them. Avoid truthiness checks if other falsy values are valid in your context.
