Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 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

0 votes
2 answers
asked Jul 13, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
+1 vote
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...