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

Runtime error messages

In the following error messages, xyz denotes variable text that JavaScript provides, such as a function or variable name. These errors occur at runtime -- that is, after the script has been loaded and code is executed.

Note: The xyz identifier may not always appear in the error message. For example, instead of saying "myObject cannot be indexed as an array," the error message may simply say, "cannot be indexed as an array." Fortunately, JavaScript does provide the number of the approximate line that contains the error, which can help in tracking down the source of a problem.

xyz cannot be indexed as an array

Only objects can be indexed as an array, and this error typically occurs when you attempt to use a non-object as an array (the error also occurs if you attempt to use an array not supported by a given object, such as document.elements[0]). The following results in an error because window.length is a property, and not an object.

alert (window.length[0]);

However, the following does work because a number of properties are contained in the document object. The array index values access each of the available properties.

alert (document[0]);

xyz has no properties

The object you've referenced has no properties. This is a common error, and is usually the result of misusing objects. This error will crop up, for example, with the following:

document.history.go(1);

The error occurs because history is a property of a window, not a document. The correct syntax is:

window.history.go(1)

xyz is not defined

This error occurs when your JavaScript code references a variable or object that does not exist. For example, you get the error with the following. (Note that the assigned variable is Test, whereas the variable used with the alert method is Text.)

var Test="This is a test;
alert (Text);

Another common mistake is to use the wrong capitalization when referring to variables and objects. This code results in an error, even though you have spelled the variable name correctly:

var Test="This is a test;
alert (test);

Conclusion

Debugging JavaScript programs may not be state-of-the-art (yet, anyway). Nevertheless, with a bit of sleuthing, and a handful of simple tools and techniques, you can debug most any JavaScript coding problem you may encounter. And as you might expect, the process gets easier as you become more experienced at programming in JavaScript.

About the author

Gordon McComb is an author, consultant, and lecturer. He has written 50 books and more than a thousand magazine articles during his 20 years as a professional writer. More than a million copies of his books are in print. Gordon also writes a weekly syndicated newspaper column on computers, reaching several million readers worldwide. Gordon's latest book is The JavaScript Sourcebook, forthcoming from Wiley Computer Publishing.

Read more about Tools & Methods in JavaWorld's Tools & Methods section.

  • Print
  • Feedback