Next page | Contents page |

What if it doesn't work? - The console

As mentioned in a side box on the previous page, the syntax of a programming language is strict. You must use the right letter cases and punctuation. The type of brackets matters: function parameters are in smooth brackets, or parentheses: () but function bodies are in curly braces: {}. In some fonts it can be hard to tell them apart but it does matter.

If you have checked all that, the next thing to do is to look for your browser's Error Console, which is likely to pop up as a separate window.

Once you have an error console, clear its contents (there will be a button). Then try to view the problem page again. The error console should then show any errors that occurred and probably give you the line number in the .js file at which the problem was detected. That does not mean that particular line is wrong but it usually gives a clue as to what is wrong.

Another thing you can do if your program is partially working is to insert an extra line to pop a message window up, so you can tell whether execution is getting to that point:


  alert ("My message");

Click this button to see what an alert looks like in your system:

Since I first wrote this course most browsers have adopted what is known as the Console API. It has several useful functions you can use for debugging and testing. For example


  console.log (x);
where x is any of the variables in your code, enables you to see full details of the variable in the browser's console.

Other particularly useful functions are console.debug () and console.assert () but there are several others. For details see Mozilla's reference page about the Console.

Next page | Contents page |