Print this Section

  
  

Error Messages

When writing JavaScript programs, it is inevitable that errors will occur. It is important to be familiar with the different types of JavaScript errors so that every effort can be made to detect, isolate, and correct the problems before the applications are made accessible to the public.

JavaScript programs can produce the following types of errors:

  • Syntax Errors - errors that violate the rules of JavaScript. Examples include, missing braces in the program, missing semicolons at the end of the statement, or misspelling a keyword. Syntax errors are usually easier to find and correct and other types of errors.
  • Run Time Errors - errors that occur during the execution of the JavaScript program. Examples include, running out of memory or using an undefined variable.
  • Logical Errors - errors that cause the JavaScript program to perform incorrectly. A logical error produces unintended or undesired output. A logical error can occur when an incorrect formula is used to produce a desired result.

The first step in debugging JavaScript programs is to identify and understand the error messages generated when the JavaScript interpreter encounters a syntax or run-time error. Fortunately, modern browsers include JavaScript consoles that enable you to view a description of the error and the line number in the document where the error occurred.

Firefox JavaScript Console

Use the following steps to access the console in Firefox.

  1. Open Firefox
  2. Go to Tools -->Web Developer --> Web Console
  3. Go to the page containing the error. The error message and line number will appear in the console.

Internet Explorer JavaScript Console

Use the following steps to access the console in IE.

  1. Open IE
  2. Go to Tools --> F12 developer tools
  3. Go to the page containing the error. The error message and line number will appear in the console.

Chrome JavaScript Console

Use the following steps to access the console in Chrome.

  1. Open Chrome
  2. Click the "Customize and Control Google Chrome" button at the top right side
  3. From the drop menu, go to Tools-->JavaScript console

Safari JavaScript Console

Use the following steps to access the console in Safari.

  1. Open Safari
  2. Click the "Display a menu of general Safari settings" button at the top right that looks like a gear
  3. Click preferences
  4. On the Advanced tab, check the "Show Development menu in menu bar" box
  5. Click the "Display a menu for the current page" icon towards the top right that looks like a paper with a bent corner.

At this point the goal is to be able to use the Web browser to help identify errors that are likely to occur as you begin coding your JavaScript applications. Later, in Tutorial 11, we will look at advanced error handling techniques in more detail.


TOP | NEXT: Chapter 2 - JavaScript Data Operations