Program errors

The unfortunate reality with programming, in any language, is that you are going to make mistakes. Program errors usually consist of typographic, syntax or run-time errors. Typographic errors are relatively easy to fix, requiring only a retype of the offending phrase. A syntax error is a little more complicated, occurring when you use a command or phrase in a way that it wasn't intended to be used. Sometimes experimentation and a few trials will reveal the correct approach, but at other times you will have to refer back to the language's documentation. The worst errors, at least in terms of correcting, are run-time errors. A run-time error is a flaw in a program's internal logic. For example, a program might input two numbers and then prompt for instructions on whether to add or subtract the numbers. If the program always subtracted the numbers, even when instructed to add them, the results would be incorrect for each addition request. This example would probably be fairly easy to isolate and correct, but others can be decidedly more complex and more difficult to locate.

The good news is that you're going to make lots of errors. It is likely that every piece of code you create will contain at least one error. How is that good news? The more errors you receive and fix, the better you will become at correcting them. Errors are annoying at best, and part of the challenge of programming. The other good news is that your browser will report most errors and the line that caused the problems.

Taking a look at the example again; if we take out the first double-quotation mark in the line:

document.write("The value of x is ");

we have,

document.write(The value of x is ");

Loading the page up into a browser presents an informative message. The error message displays the file with the error (C|APC/JavaScript Column/Examples/Column 2/Variable.htm), the line the error was found on (line 20), the error message ("missing ) after argument list"), and a coloured version of where the error appears.

Notice that the browser does a good job of locating the error, but fudges a bit on what the error is exactly. Actually, the browser is just assuming that everything is okay until it reaches a problem -- no closing parenthesis. Because the program encountered a double quote, it assumed that the quotation mark was an opening quote and that the parenthesis was part of the string, and not the end of the statement. The point is that error messages will often almost tell you what's wrong. However, with experience in deciphering error messages and more JavaScript knowledge, handling errors will become easy business.