How do I determine if variable is 'undefined' or 'null'. My code is as follows:
EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
///DO SOMETHING/////
};
How do I determine if variable is 'undefined' or 'null'. My code is as follows:
EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
///DO SOMETHING/////
};
Use typeof:
if (typeof(EmpName) === 'undefined') {
// blah blah blah
}
You can do like this:
if(typeof variable_here == 'undefined'){
// your code here.
};
jQuery attr()
function returns either a blank string or the actual value (and never null
or undefined
). The only time it returns undefined
is when your selector didn't return any element.
So you may want to test against a blank string. Alternatively, since blank strings, null and undefined are false-y, you can just do this:
if (!EmpName) { //do something }