Top 10 Most Common JavaScript Error Messages

By Paul McFedries

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

  • Syntax error: This 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.
  • Expected ( or Missing (: These messages mean that you forgot to include a left parenthesis:
function changeBackgroundColor newColor) {

If you forget a right parenthesis, instead, you’ll see Expected ) or Missing ):

function changeBackgroundColor (newColor{
  • Expected { or Missing { before function body: These errors tell you that your code is missing the opening brace for a function:
function changeBackgroundColor (newColor)
statements
}

If you’re missing the closing brace, instead, you’ll see the errors Expected } or Missing } after function body.

  • Unexpected end of input or Missing } in compound statement: These messages indicate that you forgot the closing brace in an if() block or other compound statement:
if (currentHour < 12) {
console.log("Good morning!");
} else {
console.log("Good day!");

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

  • Missing ; or Missing ; after for-loop initializer|condition: These errors mean that a for() loop definition is missing a semicolon (;), either because you forgot the semicolon or because you used some other character (such as a comma):
for (var counter = 1; counter < 5, counter++) {
  • Unexpected identifier or Missing ; before statement: These errors 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 semicolon (;), but this is optional. So if JavaScript thinks you haven’t finished a statement properly, it assumes it’s because a semicolon 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 defined: This 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 grossProfit = 100000;
var profitSharing = grossPrifit * profitSharingPercent;
  • X is not an object or X has no properties: 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 if you misspelled the object or property or, for the second case, that you’re using the wrong object:
document.alert("Nope!")
  • Unterminated string constant or Unterminated string literal: 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 website!
  • A script on this page is causing [browser name] to run slowly. Do you want to abort the script? 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.