Wednesday, December 26, 2018

javascript - 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

};










But if I do this, the JavaScript interpreter halts execution.


Answer



You can use the qualities of the abstract equality operator to do this:



if (variable == null){
// your code here.
}


Because null == undefined is true, the above code will catch both null and undefined.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...