Often people ask me some tricky (!) Javascript questions and most of the time same questions come back to me from different people. I often think that I should log down those “Freqently Asked Questions” in my blog with their solutions - If it helps someone someday! Also if any of these questions comes to me later I can point to my respective blog post. I don’t want to repeat myself :P
But that never happened until now. From now on I will seriously blog about Javascript. I will start this JS-FAQ series with a very simple question – how to check if a variable is undefined or empty in Javascript?
To check if the variable myVar is empty, we can write a simple condition like this,
if(!myVar) {
// if empty
}The above condition will return true, if any of the following is true for the variable myVar.
// myVar is declared but not assigned (implicitly assigned to 'undefined' by JS interpreter)
var myVar;
// myVar is declared and assigned to undefined
var myVar = undefined;
// myVar is declared and assigned to null
var myVar = null;
// myVar is declared and assigned to 0
var myVar = 0;
// myVar is declared and assigned to false
var myVar = false;
// myVar is declared and assigned to empty String
var myVar = ‘’;
Note, this condition even returns true when myVar is assigned to undefined. But unfortunately, this condition would throw an error if myVar is undeclared (as the Javascript interpreter will not find any reference of the variable). So, how to check if a variable is undeclared?
When we are referencing a variable that is not declared yet, the Javascript interpreter will assign that variable’s type to undefined (it's quite logical as there’s no way the interpreter can guess the type of a variable which is not declared). We can check if a variable is not declared yet by using the typeof command,
if(typeof myVar === ‘undefined’) {
// if undefined
}The above condition will return true, if any of the following is true,
//var myVar // myVar is not decalred yet
var myVar = undefined; // myVar is declared and assigned to undefined
We just need to combine these two conditions to find out if a variable is undefined and empty,
if(typeof myVar === ‘undefined’ || !myVar) {
// myVar is undefined or empty
}Same way, we can find out if a variable is not undefined and not empty,
if(typeof myVar !== ‘undefined’ || myVar) {
// myVar is defined and not empty
}(P.S. We must put the 'undefined check' first; otherwise as you have guessed already -it will throw an error when myVar is not declared)
Reader Commentary
5 peoples have commented so far.
Md. Mahmud Ahsan
Saturday, September 6th, 2008 at 11:57amNice Post!
Ahsan
Saturday, September 6th, 2008 at 5:29pmIs it my bad memory or what, I always forget this when I need it. Thanks for writing it down. Now I know where to look if I forget this again :)
Looking forward to this JS FAQ series!
KattyBlackyard
Sunday, June 14th, 2009 at 3:26amHi, gr8 post thanks for posting. Information is useful!
Jan Limpens
Saturday, July 18th, 2009 at 1:04amshouldn't this read
if(typeof myVar !== ‘undefined’ ∧
// myVar is defined and not empty
}
Jan Limpens
Saturday, July 18th, 2009 at 1:05amare my chars, but it should be && (amp amp) not || (pipe pipe)