Error Messages

The best way to avoid errors in scripts is to use a text editor without any margins, allow each line of JavaScript to stay on its own line, and not break long lines into two lines. There are two types of error messages:

  1. Syntax Error:

  2. Runtime Error:

I truncated the second line of the example script below to make it two lines. The browser displayed absolutely nothing at all, even though the first and third lines are correct. The error of the second line stopped the script entirely!

In 1999, I would have received error message alert windows about this, but not nowadays. Browsers have Developer's Tools, so I looked at the Console section of each. Internet Explorer had nothing at all about errors (at least I couldn't find anything). Both Google Chrome and Mozilla Firefox indicated a syntax error on line 72. Chrome reported "Uncaught SyntaxError: Unexpected token ILLEGAL" with a link to basic5.html:72 while Firefox reported "SyntaxError: unterminated string literal" with a link to basic5.html:72:16. The space before the first double quotation mark is the 16th character on the second script line. Now, that is precise!

HTML file: Displayed by browser:
<BODY>

<SCRIPT LANGUAGE="javascript">
document.write( "Let us make an error. " );
document.write( "This is an
error, " );
document.write( "but this one is not." );
</SCRIPT>

</BODY>

Interesting! Just the one error in the middle of the script can mess up the whole script! In my attempt to create errors, I first put multiple spaces between the double quotation marks and parenthesis but they were ignored and reduced to a single space by the browser. The script still worked. Then I used a <BR /> flag to break the second line and make it an error. When I commented out (/* */) the second (and third) lines, the script worked, displaying the first and fourth lines. And I've just learned that Firefox is the best tool to debug with.

BackTable of ContentsNext