HTML and CSS Reference
In-Depth Information
Figure 2.3 The output from Example 2.2 demonstrates the difference between the
document.write() and document.writeln() methods.
2.4 About Debugging
Have you ever tried to draw a picture or do your resume for the first time without a mis-
take either in the layout, order, type, style, or whatever? In any programming language,
it's the same story, and JavaScript is no exception. It's especially tricky with JavaScript
because you have to consider the HTML as well as the JavaScript code when your page
doesn't turn out right. You might get errors on the console or get a totally blank page.
Finding errors in a script can get quite frustrating without proper debugging tools.
Before we go any further, this is a good time to get acquainted with some of the types of
errors you might encounter.
2.4.1 Types of Errors
Load or Compile Time. Load-time errors are the most common errors and are
caught by JavaScript as the script is being loaded. These errors will prevent the script
from running at all. Load-time errors are generally caused by mistakes in syntax, such
as missing parentheses in a function or misspelling a keyword. You might have typed a
string of text and forgotten to enclose the string in quotes, or you might have mis-
matched the quotes, starting with single quotes but ending with double quotes.
Runtime. Runtime errors, as the name suggests, are those errors that occur when the
JavaScript program actually starts running. An example of a runtime error would be if
your program references an object or variable that doesn't exist, or you put some code
between the <head></head> tags and it should have been placed within the
<body></body> tags, or you referenced a page that doesn't exist.
Logical. Logical errors are harder to find because they imply that you didn't antici-
pate an event or that you inadvertently misused an operator, but your syntax was okay.
Search WWH ::

Custom Search