Home »
JavaScript Examples
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
Solution for "Is there a standard function to check for null, undefined, or blank variables in JavaScript?"
Submitted by Pratishtha Saxena, on April 30, 2022
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
In JavaScript, there is not a standard function using which we can check whether the variable is null, undefined, or blank.
Except we can check it by following way:
if (variable){
}
This will give the true or false value of the given variable. If the variable is null, undefined, or blank. It will give False else it will return True. Using this we can check the variable.
Let's have a look at the following examples for better understanding.
Example 1
var a = 'John';
if (a) {
console.log("It's Not Null, Undefined, Blank");
} else {
console.log("It's Null, Undefined, Blank");
}
Output:
It's Not Null, Undefined, Blank
Example 2
var a = '123';
if (a) {
console.log("It's Not Null, Undefined, Blank");
} else {
console.log("It's Null, Undefined, Blank");
}
Output:
It's Not Null, Undefined, Blank
Example 3
var a = ' ';
if (a) {
console.log("It's Not Null, Undefined, Blank");
} else {
console.log("It's Null, Undefined, Blank");
}
Output:
It's Not Null, Undefined, Blank
Example 4
var a = '';
if (a) {
console.log("It's Not Null, Undefined, Blank");
} else {
console.log("It's Null, Undefined, Blank");
}
Output:
It's Null, Undefined, Blank
Example 5
let b;
if (b) {
console.log("It's Not Null, Undefined, Blank");
} else {
console.log("It's Null, Undefined, Blank");
}
console.log(b)
Output:
It's Null, Undefined, Blank
Example 6
var a;
if (a) {
console.log("It's Not Null, Undefined, Blank");
} else {
console.log("It's Null, Undefined, Blank");
}
Output:
It's Null, Undefined, Blank
JavaScript Examples »