ADVERTISEMENT
ADVERTISEMENT

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

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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.