Question : When to check if an object exist in javascript?

Hello guys,

I was wondering, when do we need to check if an object exist in Javascript? is that really necessary? I am just asking because I am not seeing js codes making this kind of checks, so I wonder when do we need to do this. Thanks very much,

Winter

Answer : When to check if an object exist in javascript?

I would suggest checking for an object it it is at all possible that the object does not exist.

For example, whenever I have

  x = document.getElementById( 'idAttribute' );

I verify that x is not null.

However, you will find lots, and lots of JavaScript on the web where this kind of checking is not done.  In these cases, you (or the user) are likely to see a message indicating that "x" is not an object.

I tend to be paranoid, and have code to verify values before they are used.
Random Solutions  
 
programming4us programming4us