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 4 of 7

You may find it helpful to print your JavaScript programs in a monospaced font, such as Courier, to help spot such mistakes as a string that is empty but should contain a space. The monospace font is better suited to locating these kinds of errors, but it does takes up more space on the line. Use a small 9- or 10-point font to compensate.

Use a simple object inspector

The for/in statement can be used to create a simple object inspector. A short script is all you need to inspect a JavaScript object. For example, to get a listing of the current properties of the window object, load the script and type window in the prompt box. Choose OK. JavaScript displays an alert box listing each property of the window object.

<SCRIPT>
ret = prompt ("Enter object", "document");
obj = eval(ret);
var temp = "";
for (x in obj)
    temp += x + ": " + obj[x] + "\n";
alert (temp);
</SCRIPT>

Six mistakes everyone makes

Here are a half-dozen of the most common mistakes people make when writing JavaScript programs. These are listed in no particular order.

1. Missing quotation marks for strings

This should go without saying, but I'll say it anyway: Remember to provide quotes (either single or double) for all strings. The obvious use -- and most common site of abuse -- is the document.write method.

document.write(<H1>This is a heading</H1>);

This will cause a load-time error when the browser tries to load the script, because quotes are missing within the document.write statement. JavaScript doesn't know quite what's wrong here; it just tells you there's a syntax error and leaves it to you to figure out what's wrong. Add quotes to fix.

2. Mismatching quotation marks

JavaScript lets you use the single quote (') or the double-quote (") to delineate strings. However, you must be careful to stick with the same type, or else JavaScript will render an error message. The following is not allowed, because the characters used for the quote symbol are not consistent.

document.write("<H1>This is a heading</H1>');

JavaScript lets you "embed" a quoted string within another. In this case, you will want to use both quote types. However, keep in mind that for every type of quote that starts the string, you must have a matching end quote. Here's an example. Notice that the entire string is enclosed in single quotes, and the "internal" string is enclosed in double quotes. The result is a heading that appears as Ways to "Improve" Your JavaScript Programs.

document.write('<H1>Ways to "Improve" Your JavaScript Programs</H1>');

3. Using single equals instead of double equals in comparison expressions

This mistake is so common (unless you're a dyed-in-the-wool C programmer) you're bound to do it at least once. JavaScript expects to see two equals signs in a comparison expression, not one. This is a mistake:

if(MyVar = "xyz")

4. Referencing objects that don't yet exist

Navigator processes a page in the order it receives its HTML markup. It is common to place JavaScript code at the beginning of the page (usually in the <HEAD> tag). This can lead to errors if you attempt to reference some other part of the page that hasn't been loaded. For example, the following JavaScript attempts to reference a form control when the page is loaded. This reference comes before the control has been defined, so an error occurs.

  • Print
  • Feedback