How can I determine if a variable is 'undefined' or 'null'?
Asked 07 September, 2021
Viewed 1K times
  • 58
Votes

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.

30 Answer