Chapter 17. Troubleshooting and Debugging

Everybody makes mistakes, but in JavaScript, mistakes can keep your programs from running correctly—or at all. When you first start out with JavaScript, you’ll probably make a lot of mistakes. Trying to figure out why a script isn’t behaving the way it should can be frustrating, but it’s all a part of programming. Fortunately, with experience and practice, you’ll be able to figure out why an error has occurred and how to fix it.

This chapter describes some of the most common programming mistakes, and, more importantly, teaches you how to diagnose problems in your scripts—debug them, as programmers say. In addition, the tutorial will take you step by step through debugging a problematic script.

Top JavaScript Programming Mistakes

There are countless ways a program can go wrong, from simple typos to more subtle errors that only pop up every now and again. However, there are a handful of mistakes that routinely plague beginning (and even advanced) JavaScript programmers. Go over the list in this section, and keep it in the back of your mind when programming. You’ll probably find that knowing these common mistakes makes it a lot easier to identify and fix problems in your own programs.

Non-Closed Pairs

As you’ve noticed, JavaScript is filled with endless parentheses, braces, semicolons, quotation marks, and other punctuation. Due to the finicky nature of computers, leaving out a single punctuation mark can stop a program dead in ...

Get JavaScript & jQuery: The Missing Manual, 3rd 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.