Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (47.6k points)

How do I determine if the 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 the execution.

1 Answer

0 votes
by (106k points)
edited by

To determine if the variable is 'undefined' or 'null' you can use the qualities of the abstract equality operator to do this:

if (variable == null){ 

// your code here. 

}

The above code will catch both null and undefined because null == undefined is always true.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...