Newsletter sign-up
View all newsletters

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

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

Page 7 of 7

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.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (12)
Login
Forgot your account info?

CKJCFJK XFJFJU XJUTYJLOGUBy Anonymous on August 10, 2010, 12:39 pmhollister uk hollister hollister clothing hollister clothes XJH?BGHIKLUCYBNKVFGYKK?HYUIKYT hollister london hollister sale hollister outlet hollister online

Reply | Read entire comment

JNXDRTGR JXTR ??ZEGTFZAFBy Anonymous on August 7, 2010, 5:43 amed hardy ukJEKWJFJKLEJKVJKJ ed hardy outlet ed hardy online ed hardy ukKGVJKJKERJKREJJERLKJLE

Reply | Read entire comment

HSERRD REHTHYHJNMERSGBy Anonymous on August 6, 2010, 6:02 am abercrombie london YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYJJJJJJJJJJJJJJJJJJJJ?85441 Abercrombie and fitch london Abercrombie fitch london abercrombie...

Reply | Read entire comment

JAAVBy Anonymous on August 3, 2010, 1:50 pmSDFSF

Reply | Read entire comment

javaBy Anonymous on June 25, 2010, 8:56 amdebugging and html java script function

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.