If you work in the industry, you'll find these cases when a manager comes to you angry about a broken page or irate customers. Too bad QA didn't catch the case since they didn't have (or follow) OCD-style test plans, which nobody has time to write or maintain in this crazy world of deliver-this-last-week. (Ugh, I guess so much time in this line of work has taken it's toll on my nerves...sorry folks.)
What do I mean specifically? I've included some examples below in an attempt to reveal common aspects of the problem, and some approaches to solutions.
One approach--and probably the preferred one from the procedure oriented programming crowd is to use the typeof() method to check that a...something, isn't undefined. There is an example of this approach below.
It's worth pointing out that the cases above are very simplified in that the variable declaration/definition happens very close to the use case. Where this error crops up more often is when a variable is supposed to be defined in a layer not closely associated with the use case--for example in a professional setting where the programming team and the HTML teams are separate. Team 1 is supposed to create a library or server side include that will create a variable or method which Team 2 then uses blindly.
My favorite related observed interaction:
Team 1: "It's in the design documentation! "
Team 2: "...yes, well too bad it wasn't in the instantiation when the bosses browser experienced the error ...maybe you could forward a copy of the design doc to their browser."
1 The error dialog is not enabled in IE by default. You can enable the dialog in the options page by checking the box to "notify about every error". If you are serious about your craft, I highly recommend checking this box--at least on your development machine. (Your spouse, roommate, parents, or significant others would probably be annoyed by this option so don't select it on a community machine...unless you are the anti-social type.)