Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Debugging JavaScript programs

A quick reference guide to finding errors without the aid of a debugging utility

  • Print
  • Feedback

Page 2 of 7

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.

Messsage = "This is a test";    // note three s's
document.write(Message);    // note two s's

Another type of runtime error occurs when you misapply one of JavaScript's objects. For example, this code fragment results in an error ("document cannot be set by assignment.") because you cannot assign a value directly to the document object.

document="test";

Finally, a logic error is when your script does something different than that suggested by logic. The error isn't due to a misplaced parenthesis or misapplied statement, but rather to a mistake in the way you've constructed the script. For example, a logic error occurs if you fail to anticipate the user entering invalid data. The script may fail because the data it processes is not in the proper format.

Common causes of errors

You'll find that most of the errors that beset your scripts are caused by the same, and rather simple, mistakes. Make sure you look for the following:

  • Proper variable names. Keep a sharp eye out for the variables you use and their names. Avoid giving two variables similar names, such as MyVar and MyVal. Avoid one-character differences in variable names, such as Name and Names. And, of course, be sure that the variables you use are spelled properly throughout the script. Remember: JavaScript variables are case sensitive. MyVar is not the same as myvar.
  • Proper function names. Be sure function names are spelled correctly and that they don't have any extraneous characters or spaces.
  • Unique function names. Function names can be used only once in a script (of course you can call a function any number of times). Review all functions to make sure that you haven't duplicated their names.
  • Commas for arguments. With the exception of the for statement (following C and Java practice), JavaScript uses the comma as the separator character for arguments.
  • Proper placement of braces. JavaScript uses the { and } brace characters to define a block of statements. Be sure to include all the necessary brace characters for the statement block or errors will result.
  • Quotes around strings. All strings should have quotes around them.
  • Proper script sequence. It's possible to enter a statement into a script at the wrong place while editing it, especially if you're using time-saving, cut-and-paste techniques. Double check.
  • Correct object names. Be sure names for objects are spelled exactly, and with the proper capitalization. Remember that the built-in objects (Date, Math, Array, Object, etc.) have initial caps; the others start with lowercase letters. Develop a consistent naming convention for your variables, objects, and functions.

Determining the source of a problem

An important step in repairing a broken script is isolating its various functions and routines, and analyzing it on a piece-by-piece basis. This can be done using a number of low-tech approaches, such as setting watchpoints using alert message boxes, and taking advantage of the for/in statement to peer into JavaScript's objects.

  • Print
  • Feedback