How to determine if variable is 'undefined' or 'null'?
How do I determine if variable is
undefined
or null
? My code is as follows:var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
//DO SOMETHING
};
<div id="esd-names">
<div id="name"></div>
</div>
But if I do this, the JavaScript interpreter halts execution.
Answer:
You can do this:
if (variable == null){
// your code here.
}
Check MDN for details on equality tests in JS.
null == undefined
is true, but null === undefined
is false. Thus the code above, as is, will catch both undefined
and null
.
http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null