Java Reference
In-Depth Information
Debugging in the Browser
Debugging is the process of finding out where bugs occur in the code and then dealing with
them. In many cases, the point at which an error occurs is not always where it originated, so
you'll need to run through the program to see what's happening at different stages of its ex-
ecution. When doing this, it can be useful to create what are known as breakpoints , which
halt the progress of the code and allow us to view the value of different variables at that
point in the program. There are a number of options for debugging JavaScript code in the
The Trusty Alert
The most basic form of debugging is to use the alert() method to show a dialog at certain
points in the code. Because alert() stops a program from running until OK is clicked, it
allows us to effectively put breakpoints in the code that let us check the value of variables
at that point to see if they're what we expect them to be. Take the following example that
checks to see if a person's age is appropriate:
function amIOldEnough(age){
if (age < 12) {
return "No, sorry.";
} else if (age < 18) {
return "Only if you are accompanied by an adult.";
else {
return "Yep, come on in!";
The alert method at the start of the function will allow us to see the value of the age
variable at that point; once we click on OK we can check that the correct string is returned.
Using alerts for debugging was the only option in the past, but JavaScript development has
progressed since then and their use is discouraged for debugging purposes today.
Search WWH ::

Custom Search