Use a JavaScript Debugger
A debugger is an application that places all aspects of script execution under the control of
the programmer. Debuggers provide fine-grained control over the state of the script
through an interface that allows you to examine and set values as well as control the flow
of execution.
Once a script has been loaded into a debugger, it can be run one line at a time or
instructed to halt at certain breakpoints. Once execution is halted, the programmer can
examine the state of the script and its variables in order to determine if something is
amiss. You can also watch variables for changes in their values.
The latest version of the Mozilla JavaScript Debugger (code-named Venkman) for both
Mozilla and Netscape browsers can be downloaded at
http://www.hacksrus.com/~ginda/venkman
Useful Tips for Developers
You can keep the following tips in mind to reduce the number of errors in your scripts and
simplify the debugging process −
Use plenty of comments. Comments enable you to explain why you wrote the
script the way you did and to explain particularly difficult sections of code.
Always use indentation to make your code easy to read. Indenting statements
also makes it easier for you to match up beginning and ending tags, curly braces,
and other HTML and script elements.
Write modular code. Whenever possible, group your statements into functions.
Functions let you group related statements, and test and reuse portions of code
with minimal effort.
Be consistent in the way you name your variables and functions. Try using names
that are long enough to be meaningful and that describe the contents of the
variable or the purpose of the function.
Use consistent syntax when naming variables and functions. In other words, keep
them all lowercase or all uppercase; if you prefer Camel-Back notation, use it
consistently.
Test long scripts in a modular fashion. In other words, do not try to write the
entire script before testing any portion of it. Write a piece and get it to work before
adding the next portion of code.
Use descriptive variable and function names and avoid using single-character
names.
Watch your quotation marks. Remember that quotation marks are used in pairs
around strings and that both quotation marks must be of the same style (either