The answer is feature detection. The basic premise is that you check that any features you are going to use exist before you execute the code. As a very simple example lets look at the alert function. Your code would look like follows
if (alert){
//do something
}
else{
//no alert avaliable
}
Of course alert is something that you would be unlikely to use in production, and therefore something unlikely to be tested for. However, this basic idea can be used for most situations.
For a simple implementation that works with every feature I would suggest you look at modernizer(modernizr.com). However, this may be overkill in some situations.