WebDeveloper.com �: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS Properties
HTML 4.01 Tags
Site Management
WD Forums

    Web Video
    Expression Web



    Forum, Blog, Wiki & CMS

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

    The Coffee Lounge
    Computer Issues

JavaScript Programming: Debugging Part II

by Heidi Brumbaugh

A syntax error is asking for a "scwerdriver".

A logic error is asking for a flathead screwdriver, then trying to use it on a Philips screw.

Most other programming languages have a debugging environment that helps you zero in on errors. If you're programming JavaScript from a text editor and checking the results in a browser, however, debugging is difficult. The browsers are designed to be upwards compatible, so they may simply ignore instructions they don't understand. When they do report errors, the messages don't always tell you what you need to know to solve the problem.

You can follow along with the bugs.html file, which gives examples of many of the problems I talk about here.

When the browser gives you a syntax error, note the line number of the error. When you dig back into the script to see what's wrong, however, check for problems above and around that line for the problem as well. An error above the line, especially a missing ), >, or } could be causing the browser to misinterpret what happens next.

On a related note, be aware of "cascading" errors. This means that one error, such as an incorrectly defined function, can cause errors further down the line, for example, when you try to call the function. Correct the first error, then try to run the program again. Subsequent errors may be gone.

Here are some more tips on avoiding--and troubleshooting--programming errors.

  • Be careful about using reserved words--words that are part of JavaScript itself--as variable or function names.
  • Make sure your double and single quotation marks are matched correctly, especially in long expressions.
  • When you're writing code that generates HTML or JavaScript, it can be especially easy to make a mistake. Examine your output directly by running the code to create a new document, then clicking on View Page Source in the browser to see what actually got generated.

[Move on the Part III]

Web Developer® Site Feedback
Web Developer®
Copyright © 2000 internet.com Corporation. All rights reserved.

HTML5 Development Center

Recent Articles