Debugging JavaScript: Understanding JavaScript Error Messages / Page 2 | WebReference

Debugging JavaScript: Understanding JavaScript Error Messages / Page 2


[prev]

Debugging JavaScript: Understanding JavaScript Error Messages [con't]


When writing code, pay close attention to the following:

  • Variable names: Avoid giving two variables similar names, such as MyVar and MyVal. Be sure that the variables you use are spelled correctly, as JavaScript variables are case sensitive.
  • Function names: Be sure function names are spelled correctly and that they don't have any extraneous characters or spaces. Function names can be used only once per declaration in a script. You cannot reuse the same function with different signatures. Review all functions to make sure that you haven't duplicated their names.
  • Commas for arguments: With the exception of the for statement (following C and Java practice), JavaScript uses the comma as the separator character for arguments.
  • Proper placement of braces: JavaScript uses the { and } (curly braces) characters to define a block of statements. Be sure to include all the necessary brace characters for the statement block or errors will result. One way to do this is to line up braces vertically. I am a proponent of this style:
  • Quotes around strings: All strings should have quotes around them. Make sure that you are using the same kind (single or double) at both ends!
  • Cutting and Pasting: It's easy to enter a statement into a script at the wrong place while editing it, especially if you're using time-saving, cut-and-paste techniques. Don't forget to update all the variable and function names in the block!
  • Correct object names: Be sure names for objects are spelled exactly, and with the proper capitalization. Remember that the built-in objects (Date, Math, Array, Object, etc.) start with caps; the others start with lowercase letters. Develop a consistent naming convention for your variables, objects, and functions.

Microsoft has a comprehensive listing of JavaScript error messages.

Understanding the types of errors that you are likely to encounter in JavaScript development is the first step in reducing and eliminating bugs in your code. The next step is to learn how to display error messages in a meaningful in order to better stay in control of the situation when unexpected events occur. That's what we'll be covering at next time, when we look at exception handling and browse some specialized tools to assist in writing and debugging our code.


Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: April 14, 2009

[prev]