provided by: 
Originally published at Internet.com You have written a JavaScript application and it's working fine-until you get an error message. It's something unsightly that pops up on your screen, something like 'myObject.fields is null or not an object'. What on earth does this mean? This article is going to show you how to account for errors and show you several different methods for general error handling. Syntax Errors JavaScript has two basic levels of error handling: syntax errors and runtime errors. Syntax errors occur before the JavaScript code even runs, basically meaning that the code can't compile. Take the following code, for example: for(var i=0; i<10; i++) // do stuff here } You'll notice that we are missing the opening { character. If you try to run this code you will immediately get an error message, even if the code is in a function that is not executed immediately. These errors are almost always easy to find and fix. You will get a message saying something like "Expected ')', line 10, char 18". If you go to line 10 there will usually be a fairly obvious error, such as a missing ), an extra < or any other typo you may have entered. There's nothing we can do about this type of error other than to just fix it and move on. Here is a list of some of the more common syntax errors: * Missing or Mis-Matched Braces, Parenthesis, or Brackets Every Brace, {, Parenthesis, ( or Bracket, [ must have its closing counterpart. If you have nested sets of these, the inner ones must be closed before the outer ones. For example, {[}] is invalid. Every if, for and while statement must also have its conditions surrounded by parenthesis. "if x=5{" is invalid because the "x=5" should have parenthesis around it. If you are having trouble with this, there are several editors out there, such as EditPlus that can highlight matching sets of braces, etc for you. * Missing or Mis-Matched Quotes This is a very common problem. Strings in Javascript begin or end with a ' or " and must end with the same character. If that character exists in your string, then it must be escaped. For example, the code var x = 'It's a beautiful day'; is invalid because the ' in It's is not escaped. This code should read: var x = 'It\'s a beautiful day'; // or var x = "It's a beautiful day"; Another somewhat common mistake is to close a string with the wrong character, ie: var x = "It's a beautiful day'; This string is started with a " character, so it must end with a " character. * Missingg semi-colons Although semi-colons are not usually necessary in JavaScript, it is a very good practice to always use them. If you are trying to shrink your JavaScript file, for instance, a common practice is to remove all the line breaks. Take the following code: var x=5 var y=10 If we remove the line breaks we get var x=5 var y=10 which breaks. If we had semi-colons in place this would not be an issue. Runtime Errors So we move on to runtime errors. Once you get your code to run at all, we start to encounter runtime errors. Runtime errors can occur for many, many reasons. Each of the following blocks of code will throw an error: alert(x); // 'x' is undefined var x; x[5] = 'test'; // 'x' is null or not an object window.frames = 5; // Not implemented var for; // expected identifier // object doesn't support this property or method document.doesNotExist(5); alert(parseint('5')); // object expected Most of these issues are caused by more common mistakes that you have to look out for: * Incorrect Capitilization All built-in JavaScript functions use something called Camel Case. Basically what this means is that all function names start with a lower case letter and the start of every subsequent word is upper-case: parseInt, getElementById, createElement, appendChild, etc. Since JavaScript is case-sensitive, typing one of these function names incorrectly will often result in a runtime error. * Referencing code, functions or DOM objects before they exist This problem usually occurs with respect to DOM objects. Let's say you have code that modifies some form elements on your page. If you attempt to run this code before the form elements exist, i.e. if you put it in your
tag, you will get a JavaScript error. This can usually be solved easily. The best-practice solution is to run your code on the onload event, ie: Read article at Internet.com site