18Common Mistakes, Debugging, and Error Handling


  • Spotting common mistakes that everyone makes—even pros!
  • Handling runtime errors, or exceptions, with the try…catch statement
  • Debugging JavaScript with the development tools of various browsers


You can find the wrox.com code downloads for this chapter at http://www.wiley.com/go/BeginningJavaScript5E on the Download Code tab. You can also view all of the examples and related files at http://beginningjs.com.

Even a JavaScript guru makes mistakes, even if they are just annoying typos. In particular, when code expands to hundreds or thousands of lines, the chance of something going wrong becomes much greater. In proportion, the difficulty in finding these mistakes, or bugs, also increases. In this chapter you look at various techniques that will help you minimize the problems that arise from this situation.

You start by taking a look at the top seven JavaScript coding mistakes. After you know what they are, you’ll be able to look out for them when writing code, hopefully, so that you won’t make them so often!

Then you look at how you can cope with errors when they do happen, so that you prevent users from seeing your coding mistakes.

Finally, you look at the debugging tools in Microsoft’s Internet Explorer (IE11), Firebug (an add-on for Firefox), Chrome’s Web Inspector, and Opera’s Dragonfly. You see how you can use these tools to step ...

Get Beginning JavaScript, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.