Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Debugging JavaScript programs

A quick reference guide that helps you find errors, <br>even without the aid of a debugging utility

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
They say that novels are not written, but rewritten. Certainly the same goes for computer programs, including those written in JavaScript. Bugs are the bane of any programmer, and debugging is part of the program development process.

Unfortunately, client-side JavaScript currently lacks a debugging utility, so finding errors in your script becomes a lesson in deductive reasoning. You cannot -- as yet, anyway -- set a breakpoint and have Netscape Navigator display the contents of registers or variables if a program terminates unexpectedly.

Debugging a JavaScript program entails using problem-solving techniques that would impress Sherlock Holmes. Effective debugging of JavaScript requires some experience writing programs for it. After a while, you become familiar with the error messages JavaScript displays and learn ways to solve the errors. Of course, you may not have the time to devote yourself to learning all the intricacies of JavaScript. This column is your quick-reference guide to understanding how to effectively debug JavaScript programs, even without a debugging utility.

Determining the type of error

There are three general types of errors that can occur when playing a JavaScript program. These errors are:

  • Load-time errors
  • Runtime errors
  • Logic errors


Load-time errors are those that are caught by JavaScript as Navigator loads the script. These errors are the major mistakes that prevent the script from functioning before it has a chance to start. It is during the loading process that JavaScript spots any serious errors that will cause your script to fail right off the bat. The script cannot be run until the page has been successfully loaded.

Load-time errors are perhaps the most common and are generally caused by problems in syntax. To help you determine the problem, JavaScript displays a warning box when a load-time error occurs. The warning box tells you the problem and, most of the time, shows you the actual text of the error. Bear in mind that the warning box doesn't always indicate the actual error. Depending on the problem, the error may be located at a different part of the line or even on another line.

As an example, general errors in syntax, such as forgetting to provide the proper open and close braces around a function, create a load-time error. Here's one such example (the actual error message is "missing } after function body"):


function test () { alert ("hello")


Even if your script loads without a peep, there is no guarantee that it will run smoothly. Runtime errors are those that occur when the script is actually playing. As with load-time errors, runtime errors are displayed in an alert box. The nature of the error is specified, along with a line number (which isn't always accurate), so you can hunt down the error in the same document.

Where load-time errors are generally caused by mistakes in syntax, runtime errors are most often due to improper use of commands. For instance, you will receive a runtime error if you reference a variable that hasn't been defined. The following results in a runtime error.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (4)
Login
Forgot your account info?

Very GoodBy Anonymous on October 7, 2009, 8:40 amits very nice and helpful

Reply | Read entire comment

coolBy Anonymous on September 13, 2009, 6:20 amnice work there!! thanx alot!

Reply | Read entire comment

to display first 10 odd numbers using java scriptBy Anonymous on May 4, 2009, 6:56 amsdv

Reply | Read entire comment

Its really nice and helpful for meBy Anonymous on February 12, 2009, 9:56 amIts really nice and helpful for me

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Yahoo's JavaScript pages
    http://dir.yahoo.com/Computers_and_Internet/Programming_and_Development/Languages/JavaScript/
  • Be sure to check the Release Notes for Netscape Navigator by choosing Help, Release Notes in the Navigator menu. The Release Notes contain a listing of known bugs. (The list is incomplete, however.) The known-bugs listing can help you avoid many pitfalls in JavaScript programming.