Chapter 4. Common Mistakes, Debugging, and Error Handling

Even a JavaScript guru makes mistakes, even if they are just annoying typos. In particular, when code expands to hundreds 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 will look at various techniques that will help you minimize the problems that arise from this situation.

You'll 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'll look at how you can cope with errors when they do happen, so that you prevent users from seeing your coding mistakes.

Finally, you'll look at the debugging tools in Microsoft's Internet Explorer (IE8), Firebug (an add-on for Firefox), Safari's and Chrome's Web Inspector, and Opera's Dragonfly. You'll see how you can use these tools to step through your code and check the contents of variables while the code is running, a process that enables us to hunt for difficult bugs. You'll also take a briefer look at the debugging tools available for Firefox.

D'oh! I Can't Believe I Just Did That: Some Common Mistakes

There are seven common mistakes made by programmers. Some of these you'll learn to avoid as you become more experienced, but others may haunt you forever!

You'll find it very useful in this chapter if your browser is ...

Get Beginning JavaScript®, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.