1. Home
  2. Computing & Technology
  3. JavaScript

Common Javascript Errors
Page 2

By , About.com Guide

Mismatched Quotes or Apostropies

With HTML and Javascript you can surround text strings with either quotes (") or apostrophes ('). To keep things straight particularly when you have HTML code within document.write statements you should standardize on using quotes for HTML and apostrophes for Javascript. If you do this you will have no problem with statements such as document.write('<a href="mypage.htm">'); since the quotes are fully nested within the apostrophes.

There are two possible problems here. You might accidentally leave out a closing apostrople leading to the following statements ending up as part of the text string and therefore having the code not function. The other possibility is that you might need to include an actual apostrophe in the text. In order for an apostrophe within the text to not be treated as the end of the text it needs to be "escaped" by preceding it with a backslash like this document.write('Let\'s go.');.

Leaving out a closing apostrophe or forgetting to escape one within the text both lead to a mismatch and the code not functioning correctly. You can check for this error using a printout of your code and three highlighters. Use the first colour to mark the start of text, the second to mark escaped apostrophes and the third to mark end of text and you will soon spot any that are mismatched.

Parentheses Missing Around If Condition.

When you code an if statement you need to place parentheses () around the condition(s) that you are testing with the if statement. Where you have multiple conditions that you are testing in the one if statement you may also need to surround parts of the condition test with parentheses as well in order to make sure that the conditions are evaluated in the right order. It is easy to forget to include these parentheses and if you have many of them nested within one another it is easy to lose track of how many levels of nesting that you have open.

Like with the braces around blocks of code, using a highlighting marker to mark the various levels of nesting in a really complex if statement should soon help you see if you have misplaced one or left one out.

Explore JavaScript
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Problem Solving
  5. Common Javascript Errors 2>

©2009 About.com, a part of The New York Times Company.

All rights reserved.