Here is a working demo, and we will show how to resolve particular issues with it.
Syntax Issues
Whenever you are learning a new language, you have to deal with not knowing the particulars of the syntax. This is where a good IDE is useful. Unfortunately, there don't seem to be any great options for JavaScript IDEs. Eclipse is an option, but that takes a good amount of configuring. Previously we mentioned Notepad++, which has good syntax coloring but will not tell you outright if something is wrong. If you open the javascript in firefox, and the syntax is wrong, firefox will tell you (It is in the error log -- Tools->Error Log) so as long as it has to run the code -- ie, the function that the incorrect code in is called.
So syntax support is not the best. It certainly isn't as good as .Net for C# or Eclipse for Java; which will underline every syntax error for you and tell you why it is wrong. However, syntax errors will occur less and less as you learn the language.
Look at this example of a syntax error in a program. For this you'll need to have firebug installed in firefox, and open up this page.
First, you should notice that the page doesn't do what it is supposed to. We will see what it is supposed to do in a second. If you look at the firebug (open up firebug by Tools->Firebug->Open Firebug) console, you'll notice that it says this:
missing ; before statement
[Break on this error] This is not a syntaxically correct javascript statement\n
If you click on the 'This is not a syntaxically correct javascript statement' it will bring you to the firebug script viewer, highlighting the offending line. And of course, this is not a syntaxically correct line. So we take it out like we did here, and try again.
Now this time, you'll observe nothing appears in the console until you mouse over the image. This is because the syntax error is in the mouse over function, so it doesn't know it has an error until it is called. The error is this:
thisdoesnotbelonghere is not defined
[Break on this error] thisdoesnotbelonghere
And of course, the error is simply that meaningless block of text.
Runtime Exceptions
Runtime exceptions happen when you are running a program, and you do something that the processor can't do. For instance, dividing by zero will usually cause one. Or accessing an attribute on a object that is null. These are usually pretty easy to see happen because the code will just stop and firebug will show the exception in its log.
Lets look at an example of such an exception.
If you run this page, you'll notice right off the bat, only the X direction expands correctly. Which is curious. If you look at the log you'll see:
document.getElementById("imagepanel") is null
[Break on this error] document.getElementById("imagepanel").height = y;
Which is very similiar to the line above it, except we have a typo: imagepanel needs a capital P.
Normal Bugs
And of course, not every error in our code will be seen as that by the javascript compiler. Sometimes, it will manifest as a program bug where the behavior isn't doing what we want. With some bugs, we can diagnose them by examining the code, and then comparing what we think it should do to what it does do. These bugs are usually not very severe and are often things like typos.
Some bugs though require the use of an integrated debugger where we can pause execution, and step through the code while looking at the values of different expressions. Firebug allows for this, as we will see. Check out this code.
The symptom is apparent, the X direction doesn't change, but the Y does. Lets pause the program when it gets to that function, and step through. To do this, go to the firebug pane, and look at the Script tab. It should show the first part of the HTML code. Put a break on line 6, the start of the mouse move event function. To do this, click the line number. A red dot should appear. Now, set it off by moving the mouse over the page area. If all is well, a yellow arrow should appear over the red dot. This signifies that the next line to execute is the one the arrow is pointing at.
Look at the four buttons in the top right of the firebug pane. One "continue"s the program (blue play button, hotkey is f8), one "steps into" the next function (the first one to the left of the play button, hotkey is f11), one "Steps Over" the next function (f10), and the last "Steps Out".
The continue button just resumes execution from where the yellow arrow is. The idea of taking one 'step' in code means allowing one command to execute and pausing again. If we do step into, and the next step is a user defined function, the code will step into that function and pause there. If we choose to step over, the function will execute and pause on the next line. This can save alot of time if you know some the function about to execute is correct, but don't want to resume. Stepping out will run until the current function returns, and then pause. It is by far the least used of the functions. Lets use these functions now.
Step once so that the
var X = evt.clientX;
will execute. Now hover over the X; it should display the current value of that variable. It probably isn't ten (it should be the current x local of the mouse pointer when that break was hit), which is what the width of the image seems to be. Put a break point at line 9 and resume, it should stop instantly. Highlight the part of code that says:
document.getElementById("imagePanel").width
Right click and go to 'add watch'. This should make it appear in the panel to the right, which displays the value along with the expression. The value should read as '10', which is what it looks like it is being set to. A closer examination of line 8 will show that it is using the value of the lower case 'x', which if we hover over, is in fact '10'. We are setting the upper case 'X' on line six, and since javascript is case sensitive, we get a very hard to spot error.
Note that the reason the lower case x is ten is because we set them as global variables. We also set a global variable for y, but the function will use it's own value of 'y' before using the global ones.
Knowing how to use these tools is very useful. Odds are you will spend 20% of your time writing new code, and 80% of it debugging code you've already written. You naturally want to do this as efficiently as possible.
Stacks and Consoles
We will go over two other tools, but not with any real examples.
The same pane that has the 'watch expressions' should have a tab next to watches called 'stack'. This shows your current function stack, and is useful in some instances. Your function stack is kind of like a map of the paths your current code has taken to get to where its gone; at the top of the stack is the function you are paused in. Below that is the function that called the function you are in, and it goes on like that. It is useful to see why your program is getting to a particular place.
If you look at the console pane, there is a little command line at the bottom. From there you can execute single javascript statements. With the current example loaded, go ahead and type in 'document.getElementById("imagePanel").width = 500' and hit enter. The picture should resize itself. If you hit the up arrow, it will go into the command history, so it will put 'document.getElementById("imagePanel").width = 500' back on that line. Add a zero to see the image get much bigger.
Go ahead and play with all the little tools firebug has. You should grow familiar with them. Have fun with it!