Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Everyone makes mistakes writing JavaScript programs. Errors don't know experts from novices, so the next time you get an error message while trying to play a JavaScript program you've written, don't feel bad. JavaScript provides error messages as a means to help you spot mistakes. This column describes the errors you are likely to get when writing a JavaScript program and what to do about them.
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 (circa 1996 -- ed.) 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 your browser display the contents of registers or variables if a program terminates unexpectedly.
This classic JavaWorld article, written in 1996, presents time-tested tips for understanding the nature of JavaScript bugs and working them out simply and strategically. See "Ajax: Tools of the trade" (Nate Schutta, 2009) and "JavaScript tools for the HTML5 generation" (Peter Wayner, 2012) to learn about tools that can help you write and debug modern JavaScript programs.
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.
There are three general types of errors that can occur when playing a JavaScript program. These errors are:
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"):