Avoiding != null statements The idiom I use the most when programming in Java is to test if object != null before I use it. This is to avoid a NullPointerException. I find the code very ugly, and it becomes unreadable. Is there a good alternative to this? I want to address the necessity to test every object if you want to access a field or method of this object. For example: if (someobject != null) { someobject.doCalc(); } In this case I will avoid a NullPointerException,
undefined
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 }; <div id="esd-names"> <div id="name"></div> </div> But if I do this, the JavaScript interpreter halts execution. Answer: You can do this: if (variable == null){ // your code here. } Check MDN for details on equality tests in JS. null == undefined is true, but null === undefinedis false. Thus the code above, as is, will catch