• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 12. Debugging Your Code > The Top 10 Most Common JavaScript Error Messa...

The Top 10 Most Common JavaScript Error Messages

To help you decipher the error messages that JavaScript throws your way, here's a list of the 10 most common errors and what they mean:

Syntax errorThis load-time error means that JavaScript has detected improper syntax in a statement. The error message almost always tells you the exact line and character where the error occurs (see Figure 12.1).

Expected ( (Internet Explorer) or Missing ( (Netscape)—These messages mean that you forgot to include a left parenthesis:

function calculate_profit_sharing guess) {

If you forget a right parenthesis, instead, you'll see Expected ) (Internet Explorer) or Missing ) (Netscape):

function calculate_profit_sharing (guess {

Expected { (Internet Explorer) or Missing { before function body (Netscape)—These errors tell you that your code is missing the opening brace for a function:

function calculate_profit_sharing (guess)

If you're missing the closing brace, instead, you'll see the errors Expected } (Internet Explorer) or Missing } after function body (Netscape).

Expected } (Internet Explorer) or Missing } in compound statement (Netscape)—These message indicate that you forgot the closing brace in an if() block or other compound statement:

if ((net_profit + profit_sharing) != gross_profit) {
    calculate_profit_sharing (profit_sharing)

If you forget the opening brace, instead, you'll get a Syntax error message that points, confusingly, to the block's closing brace.

Expected ; (Internet Explorer) or Missing ; after for-loop condition (Netscape)—These errors mean that a for() loop definition is missing a semicolon (;), either because you forgot the semi-colon or because you used some other character (such as a comma):

for (var counter = 1, counter < 5; counter++) {

Expected ; (Internet Explorer) or Missing ; before statement (Netscape)—These errors (particularly the Netscape message) tell you that the previous statement didn't end properly for some reason, or that you've begun a new statement with an invalid value. In JavaScript, statements are supposed to end with a semi-colon (;), but this is optional. So if JavaScript thinks you haven't finished a statement properly, it assumes it's because a semi-colon is missing. For example, this can happen if you forget to include the opening /* to begin a multiple-line comment:

Start the comment (oops!)
Close the comment */

X is not definedThis message most often refers to a variable named X that has not been declared or initialized, and that you're trying to use in an expression. If that's the case, declare and initialize the variable. Another possible cause is a string literal that isn't enclosed in quotation marks. Finally, also check to see if you misspelled the variable name:

var gross_profit = 100000
var profit_sharing = gross_prifit * profit_sharing_percent

X is not an object (Internet Explorer) or X has no properties (Netscape)These messages mean that your code refers to an object that doesn't exist, or to a property that doesn't belong to the specified object. Check to see whether you misspelled the object or property or, for the second case, that you're using the wrong object:


Unterminated string constant (Internet Explorer) or Unterminated string literal (Netscape)—Both of these messages mean that you began a string literal with a quotation mark, but forgot to include the closing quotation mark:

var greeting = "Welcome to my Web site!

A script on this page is causing Internet Explorer to run slowlyDo you want to abort the script? (Internet Explorer) or Lengthy JavaScript still running. Continue?These errors tell you that your code has probably fallen into an infinite loop. You don't get any specific information about what's causing the problem, so you'll need to scour your code carefully for the possible cause.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint